开发者

How to do ORs in MongoDB?

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 } ] } )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜