search

Home  >  Q&A  >  body text

How to query based on _id in nodejs mongodb

It is not possible to refer to the methods on the Internet.

var ObjectId = require('mongodb').ObjectID;
let _id = ObjectId(_id);
this.collection.findOne({_id:_id}, {}, function(err, doc) {
    console.log(err, doc);
});

I couldn’t find it after looking through the documents, mainly

"_id" : ObjectId("5927e105681b8b8fdb7887b9"),

The types may be different and cannot be queried. Please let me know if you know.

大家讲道理大家讲道理2761 days ago841

reply all(1)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-31 10:37:57

    A classmate has asked this question, and it is the same as what you encountered;

    var ObjectId = require('mongodb').ObjectID; //Does not work

    var ObjectId = require('mongodb').ObjectId; //Work

    Please refer to the official documentation.

    Love MongoDB! Have fun!


    Click Me <---Sign up and get a T-Shirt and other small gifts!

    The 2017 MongoDB Chinese Community Beijing User Group Conference is coming soon, June 3, 2017 13:00-18:00

    reply
    0
  • Cancelreply