简体   繁体   中英

Find by _id in mongodb in a node.js

I'm using a node.js and mongodb, and I'm trying get item by _id

  const id = "57bb26e5b2f56d2d47f5e092";
  const tree = db.collection('tree');
  var obj_id = new ObjectID(id);
  const node = tree.findOne({ _id: obj_id });

Result of this query is empty, when i'm trying make that query on mlab.com i get same result, only mongo-shell was return what i expected.

I'm trying that variants:

  1. var obj_id = ObjectId(id);

  2. var obj_id = "57bb26e5b2f56d2d47f5e092"

Still the same

What i do wrong?

You will need a callback on your findOne method per the documentation here:

http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findOne

If you are using async/await you will need to add the await keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM