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