繁体   English   中英

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

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

我试图运行查询以查找包含电话号码数组的所有文档:

例如:

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

示例文档为:

{
  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
}

因此,我试图运行查询以查找包含数组中任何这些元素的所有文档,我知道我可能可以运行for循环,但是我宁愿尝试在一个查询中进行操作。

我正在使用猫鼬节点模块查询我的结果。

例如:

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

您应该使用“ $ in”运算符:

在$

$ in运算符选择字段值等于指定数组中任何值的文档。

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

使用您的示例,这应该适合您:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM