0 votes
1 view
in SQL by (47.8k points)

Is there a function to turn a string into an objectId in node using mongoose? The schema specifies that something is an ObjectId, but when it is saved from a string, mongo tells me it is still just a string. The _id of the object, for instance, is displayed as objectId("blah").

2 Answers

0 votes
by (107k points)

To convert Node.js Mongoose.js string to ObjectId function you can do it like so:

var mongoose = require('mongoose'); 

var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');

0 votes
by (78k points)

You are looking for:

mongoose.mongo.BSONPure.ObjectID.isValid

Or

mongoose.Types.ObjectId.isValid

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...