开发者

selecting values of an array with mongodb

I have the following JSON entries on my mongodb:

{a:[10, 20, 30, 40]}
{a:[10]}
{a:[10, 20]}
{a:[10, 30]}

I want to select all 'a' that contains ((10 OR 20) AND (30 OR 40))... in this example, just first and last

I tried (without succe开发者_开发技巧ss) using $and and $or like: {$and:[{$or:[{'a':10},{'a':20}]}, {$or:[{'a':30},{'a':40}]}]}


$and only works since version 1.9.1. Check db.version() and you'll see you have a lower version than that as it is not a downloadable package yet (last stable is 1.8.2, last dev is 1.9.0). These should both do what you want if version < 1.9.1 :

 db.test.find({$or:[{a:10},{a:20}], $or:[{a:30},{a:40}]})
 db.test.find({a:{$in:[10,20]}, a:{$in:[30,40]}})
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜