node.js - Cannot applying find() method with Native MongoDB becaus of ID type -


i have function needed results.

when give 1 _id filter ok.

collectionpersonnel      .find({ '_id' : 1 })      .toarray(function (err, personnel) {      console.log(personnel);     }); 

if give filter way instance user[0]['personnel_id'] -that store 1- [] result;

collectionpersonnel      .find({ '_id' : user[0]['personnel_id'] })      .toarray(function (err, personnel) {      console.log(personnel);     }); 

and i've tried way. doesn't work because used string(user[0]['personnel_id']) instead of objectid.

var objectid = require('mongodb').objectid;  var personnelpk_hex = (user[0]['personnel_id']).tohexstring();  var personnelpk = objectid.createfromhexstring(personnelpk_hex);    

what should do?

edit

all of codes below;

module.exports = {      show: function(req, res) {          user.native(function(err, collectionuser) {             if(err) {                 console.log("there no exist user current_id");             };             collectionuser             .find({'_id' : req.param('id')})             .toarray(function (err, user) {                  personnel.native(function(err, collectionpersonnel) {                     if(err) {                         // handle error getting mongo collection                         console.log("there no exist personel current _id");                     };                     if(!collectionpersonnel) {                         console.log("there no exist personel current _id");                     };                      // var objectid = require('mongodb').objectid;                      // var personnelpk_hex = (user[0]['personnel_id']).tohexstring();                      // var personnelpk = objectid.createfromhexstring(personnelpk_hex);                                        collectionpersonnel                     .find({ '_id' : user[0].personnel_id })                     .toarray(function (err, personnel) {                          console.log(personnel);                        });                 });             });                     });     } }; 

and console's output is; []

solved

just apsillers's said. had given numeric _id collection, incorrectly. i've fixed _id value , ok.

thank all...

user[0]['personnel_id'] might string. mongo, "1" different 1, why literal number 1 worked, variable (which holds string) not.

instead, try using unary plus convert string number: +user[0]['personnel_id'].


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -