所以我有一个用于配方的模型,它具有“成分”的关系,这只是一个 ObjectIds 数组。当我在 mongo shell 上运行以下查询时,它工作正常并返回我的所有数据。
Example model :
{
"name": "...",
"_id": ObjectId("530ca903746515c0161e6b9f"),
"ingredients": [
ObjectId("53069363ff7447a81a3a7a1d"),
ObjectId("53069363ff7447a81a3a7a17")
]
}
Query:
db.drink.find({"ingredients":{"$in":[ObjectId("53069364ff7447a81a3a7a87"), ObjectId("530fb948c1a3ff480d58e43c")]}});
通过他们的水线 orm 使用sails.js,他们实际上并没有办法查询这个,或者至少通过我能找到的任何可能的谷歌搜索。因此,尝试使用本机驱动程序时,我有以下内容 -
var ings = new Array();
for (var i in req.body) {
ings.push(new ObjectID(req.body[i].toString()));
}
Drink.native(function(err, collection){
if(err){
return res.send(err, 500);
} else {
collection.find({"ingredients":{"$in":ings}}).toArray(function(err, data){
console.log(data);
});
}
});
问题是回调中返回的数据数组始终为空。如果我检查 'ings' 数组,它是一个 objectids 数组,所以我不确定为什么它不会返回任何数据。如果我在“查找”函数中删除 json 对象,它会返回所有内容。任何人都知道如何在使用sails.js 时使这个查询返回数据?