简体   繁体   English

在一个字段中按数组查找mongodb中的文档?

[英]finding documents in mongodb by array for one field?

Im trying to run a query to find all the documents that contain an array of phone numbers: 我试图运行查询以查找包含电话号码数组的所有文档:

for example: 例如:

var mobileArray = ["07958485374","07958485375", "07958485378"];

an example document would be: 示例文档为:

{
  phone_number: 07958483297,
  brand: "Mercedes",
  model: "S 350 L",
  year: 2007,
  price: 9500,
  description: "Full options - Automatic transmission - Gulf import - Excellent condition - V6 - Beig leather seats - Screen - Sunroof - CD - DVD - Telephone - Navigation - Xenon - Finger print - Bluetooth - Memory - Cruise control - Sensor - Wood - Clause examination - ",
  images_length: 4,
  _id: ObjectId("53a844d26d437e394844f0a3"),
  date: ISODate("2014-06-23T15:16:34.233Z"),
  __v: 0
}

So Im trying to run a query to find all documents that contains any of these elements in the array, I know I can probably run a for loop, but i would rather try do it in one query. 因此,我试图运行查询以查找包含数组中任何这些元素的所有文档,我知道我可能可以运行for循环,但是我宁愿尝试在一个查询中进行操作。

Im using mongoose node module to query my results. 我正在使用猫鼬节点模块查询我的结果。

for example: 例如:

 Car.find().where({"phone_number": "0795483297"}).limit(10).exec(function(err, docs) {
       res.send(docs);
  });

You should use the "$in" operator: 您应该使用“ $ in”运算符:

$in 在$

The $in operator selects the documents where the value of a field equals any value in the specified array. $ in运算符选择字段值等于指定数组中任何值的文档。

http://docs.mongodb.org/manual/reference/operator/query/in/ http://docs.mongodb.org/manual/reference/operator/query/in/

Using your example this should work for you: 使用您的示例,这应该适合您:

Car.find({"phone_number": 
            { $in: ['0795483297', '07958485375', '07958485378']}
          }).limit(10).exec(function(err, docs) {
       res.send(docs);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过数组字段选择检索MongoDB文档 - How to retrieve MongoDB documents via selection by array field 在mongodb中,最简单的方法是从文档中获取单个字段的数组 - in mongodb, whats the easiest way to get an array of a single field from the documents 通过循环另一个数组从一个数组中查找数据(MongoDB,Javascript) - Finding data from one array by looping another array (MongoDB, Javascript) MongoDB使用新的文档数组更新文档数组 - MongoDB update array of documents with new array of documents 从多个文档中获取一个数组字段并将元素合并到一个object - Get an array field from multiple documents and merge elements into one object 如果数组中存在文档字段值之一,则更新集合中的文档 - Update documents in a collection if one of the document field value exists in array 在 Node.js 的 MongoDB 文档(带有 MongoClient)中附加一个数组字段,来自另一个字段 - Appending an array field, from another field, in MongoDB documents (with MongoClient) in Node.js 在 mongodb 中查找适合 json 输入的文档 - Finding documents that fit json input in mongodb 通过 _id 在 ObjectId 类型上失败查找 MongoDB 文档 - Finding MongoDB documents by _id failing on the type of ObjectId 使用嵌套文档聚合 MongoDB 数组 - aggregate MongoDB array with nested documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM