简体   繁体   English

使用$ in的Mongoose查找数组

[英]Mongoose find array with $in

Team.find({
        '_id': { $in: [
            teamIds
        ] }
    }, function(err, teamData) {
        console.log("teams name  " + teamData);
    });

This code gives us undefined back.. But in the var teamIds is this: 这段代码给了我们未定义的返回..但在var teamIds是这样的:

545646d5f5c1cce828982eb7,
545646d5f5c1cce828982eb8,
54564af5c9ddf61e2b56ad1e,
54564c1f1de201782bcdb623,
54564d2fc660a7e12be6c7a2,
54564df985495f142c638f9f,
54564eadb511f1792c9be138,
54564ec40cf6708a2cd01c81,
54564ee495f4aea22cf23728

Does anybody see the error? 有人看到错误吗?

If teamIds is already an array, then you shouldn't wrap it in another array: 如果teamIds已经是一个数组,那么你不应该将它包装在另一个数组中:

Team.find({
    '_id': { $in: teamIds }
}, function(err, teamData) {
    console.log("teams name  " + teamData);
});

Or, if teamIds is a string of comma-separated id values, you need to convert it into an array of values using split : 或者,如果teamIds是逗号分隔的id值字符串,则需要使用split将其转换为值数组:

Team.find({
    '_id': { $in: teamIds.split(',') }
}, function(err, teamData) {
    console.log("teams name  " + teamData);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM