Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k 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").

3 Answers

0 votes
by (106k 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 (107k points)

You are looking for:

mongoose.mongo.BSONPure.ObjectID.isValid

Or

mongoose.Types.ObjectId.isValid

0 votes
ago by (2.8k points)

Node.js Mongoose.js string to Objectld functions:

var mongoose = require('mongoose'); 

varid=mongoose.Types.ObjectId('4edd40c86762e0fb12000003');

And for newer versions:

var mongoose = require("mongoose"); 

var _id = new mongoose.Types.ObjectId("64b0ee2c189286a5abc6b4ba");

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...