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