javascript - IDs in quotes when using MongoDB $setEquals -
i've got problem quoted ids in referenced array. when try this:
task.find({ game: req.user.game }).exec(function(err, task) { if(err) { console.log(err); } else { console.log(task[0].incategories); } });
it writes array of ids in quotes node.js console (["5550a9604b24bcdc1b88cc76", "5551213c35d0516807b2cd99"]
). i'm trying return task logged in user (look @ comments next console.log
commands):
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 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 ids without quotes res.json(tasks); } }); } });
value of sameelements
false, because $setequals
compares arrays, 1 quoted ids, 1 without quotes , don't know why it's happening???
try casting assignedtaskcategories
elements objectids first comparison:
console.log(profession[0].assignedtaskcategories); // output: array 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 ids without quotes console.log(tasks[0].sameelements); //true or false res.json(tasks); } });
Comments
Post a Comment