Team.find({
'_id': { $in: [
teamIds
] }
}, function(err, teamData) {
console.log("teams name " + teamData);
});
这段代码给了我们 undefined 返回..但是在 var teamIds 中是这样的:
545646d5f5c1cce828982eb7,
545646d5f5c1cce828982eb8,
54564af5c9ddf61e2b56ad1e,
54564c1f1de201782bcdb623,
54564d2fc660a7e12be6c7a2,
54564df985495f142c638f9f,
54564eadb511f1792c9be138,
54564ec40cf6708a2cd01c81,
54564ee495f4aea22cf23728
有人看到错误了吗?
最佳答案
如果 teamIds
已经是一个数组,那么您不应该将它包装在另一个数组中:
Team.find({
'_id': { $in: teamIds }
}, function(err, teamData) {
console.log("teams name " + teamData);
});
或者,如果 teamIds
是一串以逗号分隔的 ID 值,您需要使用 split
将其转换为值数组:
Team.find({
'_id': { $in: teamIds.split(',') }
}, function(err, teamData) {
console.log("teams name " + teamData);
});
关于arrays - Mongoose 用$in查找数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701096/