What's the best way to do a query like this in MongoDB?
SELECT * FROM things WHERE (a = 1 or b = 1) and (c = 2 or d = 2)
Thanks.
What's the best way to do a query like this in MongoDB?
SELECT * FROM things WHERE (a = 1 or b = 1) and (c = 2 or d = 2)
Thanks.
You can use the $or expression.
db.things.find( { $or : [ { a : 1}, { b : 1 } ], $or : [ { c : 2 }, { d : 2 } ] } )
@Drew, the correct answer for this query is the following:
db.test.find( { $or : [ { a : 1}, { b : 1 } ], $or : [ { c : 2 }, { d : 2 } ] } )
That's a = 1 or b = 1
and c = 2 or d = 2
. If you look at the code, you'll notice that you have two or clauses separated by a comma. When doing a find, the comma is effectively the and clause.
For docs on using the $or clause, see here.