How do I create an ObjectId in Mongoose 4.0.x from a Hex string?

mongoose.mongo.Types.ObjectId has no functionString or fromHexString. It seems that the new mongoose.mongo.Types.ObjectId (hexString) doesn't create an object id either.

var id = new mongoose.Types.ObjectId(hexString);
db.Record.find({_id:id }, function (err, campaign){
    if(err) console.log(err);
    callback(campaign);
});

      

+3


source to share


1 answer


I finally found the method you are looking for. The class mongoose.Types.ObjectId

has a static function named createFromHexString

that returns an instance ObjectId

.



var id = mongoose.Types.ObjectId.createFromHexString(hexString);
db.Record.findOne({_id: id}, function (err, campaign){
    if(err) console.log(err);
    callback(campaign);
});

      

+1


source







All Articles