Rahul Mankar
Rahul Mankar

Reputation: 990

Mongodb find query using $or

Please Help. I am trying to create the query in mongodb like (in MySql):

SELECT * FROM user WHERE user_id = '1' AND (type = 'admin' OR type = 'requester') ORDER BY user_id DESC

How do I convert this query into mongodb find({})?

Upvotes: 1

Views: 113

Answers (2)

CS_noob
CS_noob

Reputation: 557

Check this,

db.user.find({user_id:"1",$or:[{"type":"admin"},{"type":"manager"}]})

Upvotes: 1

Wake
Wake

Reputation: 1696

I'd use $in instead of $or since only a single field is involved:

db.user.find({
    user_id:"1",
    type: {$in:["admin","requester"]}
}).sort({"user_id":-1});

Upvotes: 1

Related Questions