Argument passed in must be a string of 24 hex characters - I think it is

后端 未结 6 2141
我寻月下人不归
我寻月下人不归 2020-12-18 19:40

I have a method to find a document in my database based on its ObjectID:

      console.log(\'id: \' + id + \' type: \' + typeof id);
      collection.findOne         


        
6条回答
  •  既然无缘
    2020-12-18 20:24

    Try this:

    var hex = /[0-9A-Fa-f]{6}/g;
    id = (hex.test(id))? ObjectId(id) : id;
    collection.findOne({'_id':new ObjectID(id)}, function(error,doc) {
      if (error) {
        callback(error);
      } else {
        callback(null, doc);
      }
    });
    

提交回复
热议问题