node.js mongodb select document by _id node-mongodb-native

后端 未结 10 1529
死守一世寂寞
死守一世寂寞 2020-12-02 08:28

I\'m trying to select a document by id

I\'ve tried:

collection.update({ \"_id\": { \"$oid\": + theidID } }

collection.update({ \"_id\": theidID }

c         


        
10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 08:52

    This the approach that worked for me.

    var ObjectId = require('mongodb').ObjectID;
    
    var get_by_id = function(id, callback) {
      console.log("find by: "+ id);
      get_collection(function(collection) {
        collection.findOne({"_id": new ObjectId(id)}, function(err, doc) {
           callback(doc);
        });
      });
    }
    

提交回复
热议问题