What's the best way to do a query 开发者_JS百科like this in MongoDB?
SELECT * FROM things WHERE (a = 1 or b = 1) and (c = 2 or d = 2)
Thanks.
@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.
You can use the $or expression.
db.things.find( { $or : [ { a : 1}, { b : 1 } ], $or : [ { c : 2 }, { d : 2 } ] } )
精彩评论