MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

MongoDB trova dove chiave è uguale a stringa dall'array

Quello che hai pubblicato dovrebbe funzionare - non è richiesto alcun ciclo. Il $in l'operatore fa il lavoro:

> db.Room.insert({ "_id" : 1, name: 'first'});
> db.Room.insert({ "_id" : 2, name: 'second'});
> db.Room.insert({ "_id" : 3, name: 'third'});
> // test w/ int
> db.Room.find({ "_id" : { $in : [1, 2] }});
{ "_id" : 1, "name" : "first" }
{ "_id" : 2, "name" : "second" }
> // test w/ strings
> db.Room.find({ "name" : { $in : ['first', 'third'] }});
{ "_id" : 1, "name" : "first" }
{ "_id" : 3, "name" : "third" }

Non è quello che ti aspetti?

Testato con MongoDB 2.1.1