user3331290
user3331290

Reputation: 1

How to write and or in same query in mongoDb?

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

Answers (2)

heinob
heinob

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

yaoxing
yaoxing

Reputation: 4193

db.emp.find({$or: [{$and: [{emp_id: {$gt: 200}}, {dept_id: 5}]}, {dept_id: {$lte: 4}}]})

read documents about comparison query operters here, and logical query operaters here

Upvotes: 1

Related Questions