Type casting to ObjectId in nodejs using mongoose
Sometime, we are unable to fetch the result from database as the object id coming from our request is of String type. So for this case, we can type cast our string parameter into ObjectId easily.
NOTE : Mongoose automatically typecast string id into objectId during query execution but sometime we have to manually convert into ObjectID.
var reqId = "any_string_objectId"; // coming from http requestvar ObjectID = require('mongodb').ObjectID; requiring mongodb driverreqId = new ObjectID(reqId); // output ObjectID
NOTE : Mongoose automatically typecast string id into objectId during query execution but sometime we have to manually convert into ObjectID.
Comments
Post a Comment