开发者

How to run this query in mongodb?

开发者 https://www.devze.com 2022-12-11 02:20 出处:网络
My doc has an array field Keys Keys1 and Keys2 are two arrays I want all the docs where Keys contains any value in K开发者_如何转开发eys1 AND any value in Keys2There\'s no great way to represent thi

My doc has an array field Keys

Keys1 and Keys2 are two arrays

I want all the docs where Keys contains any value in K开发者_如何转开发eys1 AND any value in Keys2


There's no great way to represent this query yet (as of 1.1.2) - if you ask on the list or file a feature request we can try to get something cooked up.

For now the best bet is probably to use an $in query to do half of the work:

db.test.find({keys: {$in: Keys1}})

You can do this in combination with a $where which can do the Keys2 part (but won't take advantage of an index - that's why it is good to do as much as possible with the regular query syntax). This would look something like this:

db.test.find({keys: {$in: Keys1}, $where: "for (i in this.keys) { for (j in Keys2) { if (this.keys[i] == Keys2[j]) return true;}} return false;"})

In latest versions of mongodb 2.6+ you can do this by $and operator.

db.test.find({$and:[{keys: {$in: Keys1}},{keys: {$in: Keys2}}]})
0

精彩评论

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