How to convert a string value to ObjectID
Sometimes, the ObjectID of your MongoDB document is delivered as a 24 character hexstring. You can’t just compare this hexstring to an ObjectID, because the comparison will fail.
The solution is as follows:
var mongoose = require('mongoose' ); var hexstring = '58666c89d4bc6622ed5373dc'; // convert it to ObjectID var ObjIDtype = mongoose.Types.ObjectId(hexstring);
The variable ObjIDtype now contains the value as type ObjectID and can be compared to MongoDB objectID’s.