質問

I have three documents:

{ "_id" : ObjectId("5265da546efdfb25e06ea009"), "a" : 1, "b" : 1 }
{ "_id" : ObjectId("5265daadc01c42c8f4974711"), "a" : 2, "b" : 2 }
{ "_id" : ObjectId("5265dab3c01c42c8f4974712"), "a" : 3, "b" : 3 }

I am trying find all the documents, where a and be have some given values..

e.g.   FIND all  documents 
       WHERE  
               a=123 and b=456
          OR   a=789 and b=888

I have tried this and this is not working :

db.test.find({$in:[{"a":2,"b":2},{"a":1,"b":2}]})

Most probably this syntax is wrong since I have not mentioned any field here.

How can I use $in , to query all these documents ?

This would be easy problem if a and b, were contained within another field, such as below :

{ "_id" : ObjectId("5265dbfcc01c42c8f4974713"), "t" : { "a" : 2, "b" : 2 } }
{ "_id" : ObjectId("5265dc02c01c42c8f4974714"), "t" : { "a" : 1, "b" : 1 } }
{ "_id" : ObjectId("5265dc07c01c42c8f4974715"), "t" : { "a" : 3, "b" : 3 } }

 db.test.find({t:{$in:[{"a":1,"b":1},{"a":2,"b":2}]}});
{ "_id" : ObjectId("5265dbfcc01c42c8f4974713"), "t" : { "a" : 2, "b" : 2 } }
{ "_id" : ObjectId("5265dc02c01c42c8f4974714"), "t" : { "a" : 1, "b" : 1 } }

I am not sure how to word it, but maybe I am looking for a way of applying '$in', to the root level document..

Am I missing something obvious or mongodb doesn't provide facility to query '$in' at the root level ?

役に立ちましたか?

解決

Is there something wrong with simple $or?

db.test.find({$or: [{"a": 1, "b": 1}, {"a" :2, "b": 2}]})

Mongo $in syntax looks like this:

{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }

and in your case there is no field to use.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top