[英]MongoDB - Find elements, where field is equal to at least one value at array
I have "reverse" problem to classic "find value(s) in array". 我对经典的“在数组中找到值”有“反向”问题。
I get array of ids and I want to find elements, which has field with value in this array. 我得到了ID数组,我想找到元素,该元素在此数组中具有值。
Example : 范例:
I have this in MyCollection 我在MyCollection中有这个
{a: 10}
{a: 7}
{a: 6}
{a: 15}
And I want to do query like this 我想这样查询
MyCollection.find({a: [6,15])
Which would return me 哪个会回报我
[
{a: 6},
{a: 15}
]
Please try to use $in
请尝试使用
$in
> db.testdata.find({a: {$in: [6, 15]}})
With data 有数据
> db.testdata.find()
{ "_id" : ObjectId("56e800e2abc8519548297bc3"), "a" : 10 }
{ "_id" : ObjectId("56e800e5abc8519548297bc4"), "a" : 6 }
{ "_id" : ObjectId("56e800eaabc8519548297bc5"), "a" : 15 }
And test code 和测试代码
> db.testdata.find({a: {$in: [6, 15]}})
{ "_id" : ObjectId("56e800e5abc8519548297bc4"), "a" : 6 }
{ "_id" : ObjectId("56e800eaabc8519548297bc5"), "a" : 15 }
您可以$gte
-更大, lt
-小于
db.collection.find({ "a" : { $gte: 6, $lte: 15} } );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.