javascript - 使用 MongoDB $setEquals 时引号中的 ID

标签 javascript node.js mongodb

我在引用数组中引用 ID 时遇到问题。当我尝试这个时:

Task.find({ game: req.user.game }).exec(function(err, task) {
    if(err) {
        console.log(err);
    } else {
        console.log(task[0].inCategories);
    }
});

它将引号中的 ID 数组写入到 node.js 控制台 (["5550a9604b24bcdc1b88cc76", "5551213c35d0516807b2cd99"])。但随后我尝试为登录用户返回任务(查看 console.log 命令旁边的注释):

Profession.find({ _id: req.user.profession }).exec(function(err, profession) {
    if(err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {
        console.log(profession[0].assignedTaskCategories); // output: array with quoted IDs
        var pipeline = [
            {
                '$match': { 'game': req.user.game,  }
            },
            {
                '$project': { 
                    'title': 1,
                    'game': 1, 
                    'inCategories': 1, 
                    'sameElements': { 
                        '$setEquals': [ '$inCategories', profession[0].assignedTaskCategories ] 
                    }
                }
            }
        ];
        Task.aggregate(pipeline).exec(function (err, tasks){
            if(err) {
                return res.status(400).send({
                    message: errorHandler.getErrorMessage(err)
                });
            } else {
                console.log(tasks[0].inCategories); //array with IDs without quotes
                res.json(tasks);
            }
        });
    }
});

sameElements 的值是 false,因为 $setEquals 比较数组,一个带引号的 ID,一个不带引号,我不知道为什么会发生这种情况???

最佳答案

首先尝试将 assignedTaskCategories 元素转换为 ObjectId,然后进行比较:

console.log(profession[0].assignedTaskCategories); // output: array with quoted IDs
var assignedTaskCategories = profession[0].assignedTaskCategories.map(function( category ) {
    return mongoose.Types.ObjectId(category);
});
var pipeline = [
    {
        '$match': { 'game': req.user.game,  }
    },
    {
        '$project': { 
            'title': 1,
            'game': 1, 
            'inCategories': 1, 
            'sameElements': { 
                '$setEquals': [ '$inCategories', assignedTaskCategories ] 
            }
        }
    }
];
Task.aggregate(pipeline).exec(function (err, tasks){
    if(err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {
        console.log(tasks[0].inCategories); //array with IDs without quotes
        console.log(tasks[0].sameElements); //true or false
        res.json(tasks);
    }
});

关于javascript - 使用 MongoDB $setEquals 时引号中的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187848/

相关文章:

javascript - 可选的强类型编译器

javascript - 语音合成 API 阻塞主线程

javascript - 我们可以用 Mongoose 检索 Node js中两个表的数据吗

javascript - 在Javascript中查找所有数组元素开头的字符串

javascript - 调用 JavaScript 类中的方法

javascript - 多人游戏 Action 同步

c# - 如何在 2.1 MongoDB C# 驱动程序中使用地理空间查询?

mongodb - 在 Mongoose/MongoDB 中创建多字段索引

node.js - 无需 Chrome 即可列出所有驱动插件 SDK (Nodejs/jpm)

javascript - Nodejs 表单未在 req.body.blog 中发送回任何数据