Reputation: 1
How to write and or in same query in mongoDb ? Say if I want to write below query in mongoDb how do I do that one.
select * from emp where (empid > 200 and dept_id=5) or dept_id <=4;
if collection holds below column
emp_id,emp_name,dept_id,sal
Upvotes: 0
Views: 120
Reputation: 19474
Try this:
db.coll.find({
$or: [
{
$and: [
{
emp_id: { $gt: 200 },
},
{
dept_id: 5
}
]
},
{
dept_id: { $lte: 4 }
}
]
})
or more easy (in this case):
db.coll.find({
$or: [
{
emp_id: { $gt: 200 },
dept_id: 5
}
{
dept_id: { $lte: 4 }
}
]
})
Upvotes: 0