$ in在环回中不起作用

我有一个string数组,如下所示:

['57b69c9d4ae615ef0e312af6','57b69bf477b8e5cd0eb38c88']; 

我将它转换为ObjectId,如下所示:

 var objectIds = []; for(var i=0; i<expenseIds.length; i++){ var _id = new ObjectId(expenseIds[i]); objectIds.push(_id); } 

的ObjectID:

 [ 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 

如果你正在使用mongoose然后尝试这个

 var objectIds = []; for(var i=0; i<expenseIds.length; i++){ var _id = mongoose.Types.ObjectId(expenseIds[i]); objectIds.push(_id); } 

或者只是你可以使用MongoDb来做到这一点

 var ObjectId = require('mongodb').ObjectID; var objectIds = []; for(var i=0; i<expenseIds.length; i++){ var _id = ObjectId(expenseIds[i]); objectIds.push(_id); } 

请让我知道如果这解决了你的问题

您需要为您的模型启用allowExtendedOperators

 //model.json ... "options": { "validateUpsert": true, "mongodb": { ... "allowExtendedOperators": true } }, ... 

UPDATE

此外,您的filter也有问题。

 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); } });