mongodb Mongoose用$查找数组in

wz3gfoph  于 2022-11-03  发布在  Go
关注(0)|答案(2)|浏览(195)
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

有人看到错误了吗?

guicsvcw

guicsvcw1#

如果teamIds已经是一个数组,那么你不应该把它 Package 在另一个数组中:

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

t30tvxxf2#

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

“这将删除逗号分隔值中的空格”

相关问题