简体   繁体   English

MongoDB-查找元素,其中字段至少等于数组中的一个值

[英]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.

相关问题 mongodb 查找查询过滤器,其中属性至少等于数组中的一项 - mongodb find query filter where a property is at least equal to one of the items in array 如何从mongodb中选择一个字段不等于某个特定值的所有文档 - how to select all documents from mongodb where one field does not equal a certain value Mongo聚合; 匹配字段的子字段至少有一个大于指定值的数组元素 - Mongo aggregation; match where a field's subfield has at least one array element greater than specified value 如何设置一个 arrays 的所有元素必须至少等于 JavaScript 中另一个数组的一个元素的条件? - How to set up consition that all elements of one arrays must be equal to at least one element of the another array in JavaScript? 使用 Dexie,我可以获取表中的所有对象,其中数组字段具有特定值作为其元素之一吗? - With Dexie, can I get all objects in a table where an array field has a specific value as one of its elements? 在angularjs中找到数组中的最小值 - find the least value in a array in angularjs 更新所有文档,设置值等于数组的值,其中索引取自另一个值(MongoDB) - Update all documents setting a value equal to value of an array where index taken from another value (MongoDB) 如何从给定的项目数组中找到其数据与至少一个项目的条件匹配的文档 - mongoDB - How to find documents that their data match a condition on at least one item from a given array of items - mongoDB 是的:确保数组中的至少一个元素是有效的 - Yup: Ensure at least one of the elements in an array is valid 查询一个字段等于另一个数组字段? - Query one field equal to another array field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM