猫鼬用$ in查找数组 [英] Mongoose find array with $in

查看:78
本文介绍了猫鼬用$ in查找数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此代码为我们提供了未定义的答案..但是在var teamIds中是这样的:

This code gives us undefined back.. But in the var teamIds is this:

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

有人看到错误了吗?

推荐答案

如果teamIds已经是一个数组,则不应将其包装在另一个数组中:

If teamIds is already an array, then you shouldn't wrap it in another array:

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

或者,如果teamIds是一串用逗号分隔的id值,则需要使用split将其转换为值数组:

Or, if teamIds is a string of comma-separated id values, you need to convert it into an array of values using split:

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

这篇关于猫鼬用$ in查找数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆