Node.js Mongoose.js string to ObjectId function

Node.js Mongoose.js string to ObjectId function

Asked on January 3, 2019 in MongoDB.
Add Comment


  • 3 Answer(s)

    The following code will be solve your issue

    var mongoose = require('mongoose');
    var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
    
    Answered on January 3, 2019.
    Add Comment

    You can try to do:

    var mongoose = require('mongoose');
    var _id = mongoose.mongo.BSONPure.ObjectID.fromHexString("4eb6e7e7e9b7f4194e000001");
    

    Update: Newest standard has fromHexString rather than fromString

    Answered on January 3, 2019.
    Add Comment

    You can just look this one:

    mongoose.mongo.BSONPure.ObjectID.isValid
    
    

    Or Else

    mongoose.Types.ObjectId.isValid
    
    
    Answered on January 3, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.