javascript - db.collection findOne by id runs but doesn't return -


i'm using node mongo db native module , i'm having difficult findone.

when run:

let collection = db.collection('eventschemas')  collection.find({}).toarray(function(error, result) {   if (error) console.log(error);   console.log(result); }); 

the result be:

[ { _id: 56b644a1bc8f66ce59322b38,     type: 'events',     attributes:       { hiw: [object],        title: 'pub crawl',        'meeting-point': 'ponto de encontro',        information: '1h de open bar',        'women-price': 40,        'men-price': 60,        end: sat feb 13 2016 17:08:00 gmt-0200 (brst),        start: sat feb 13 2016 17:07:59 gmt-0200 (brst),        'updated-at': sat feb 06 2016 17:08:54 gmt-0200 (brst),        'created-at': sat feb 06 2016 17:08:17 gmt-0200 (brst),        'is-active': true },     __v: 0 } ] 

i have 1 event, it's right result. when run:

let collection = db.collection('eventschemas')  collection.findone({ _id: '56b644a1bc8f66ce59322b38' }, function(error, result) {   if (error) console.log(error);   console.log(result); }); 

the result null, doesn't make sense. missing something?

thanks.

try wrapping _id string in objectid():

var objectid = require('mongodb').objectid,     id = new objectid('56b644a1bc8f66ce59322b38'); // wrap in objectid  let collection = db.collection('eventschemas')  collection.findone({ _id: id }, function(error, result) {     if (error) console.log(error);     console.log(result); }); 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -