node.js 猫鼬用 $in 查找数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26701096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:04:02  来源:igfitidea点击:

Mongoose find array with $in

arraysnode.jsmongodbmongoose

提问by Bart

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?

有人看到错误吗?

回答by JohnnyHK

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