我有一个字符串数组,如下所示:
['57b69c9d4ae615ef0e312af6','57b69bf477b8e5cd0eb38c88'];
我将其转换为 ObjectId,如下所示:
var objectIds = [];
for(var i=0; i<expenseIds.length; i++){
var _id = new ObjectId(expenseIds[i]);
objectIds.push(_id);
}
对象ID:
[ 57b69c9d4ae615ef0e312af6, 57b69bf477b8e5cd0eb38c88 ]
现在我在 mongoDb 中使用 $in
查询来获取所有详细信息,如下所示:
app.models.xxxxx.find({"_id" : {"$in" : objectIds}}, function(err, res){
if(err){
} else {
console.log(res);
}
});
但它没有过滤。 xxxxx 集合中的所有文档正在返回。请分享您的想法。提前致谢。
编辑: 当我在 mongo shell 中运行命令时:
db.xxx.find({ _id: { '$in': [ 57b69c9d4ae615ef0e312af6, 57b69bf477b8e5cd0eb38c88 ] }});
它抛出错误:
SyntaxError: identifier starts immediately after numeric literal @(shell):1:35
最佳答案
您需要为您的模型启用allowExtendedOperators
。
//model.json
...
"options": {
"validateUpsert": true,
"mongodb": {
...
"allowExtendedOperators": true
}
},
...
更新
您的过滤器也有问题。
app.models.xxxxx.find({where: {"_id" : {"$in" : objectIds}}}, function(err, res){
if(err){
} else {
console.log(res);
}
});
您还使用内置运算符:
app.models.xxxxx.find({where: {id : {inq : objectIds}}}, function(err, res){
if(err){
} else {
console.log(res);
}
});
关于arrays - $in 在环回中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39266997/