Node.js Mongoose.js string to ObjectId function

Mongodbnode.jsMongoose

Mongodb Problem Overview


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").

Mongodb Solutions


Solution 1 - Mongodb

You can do it like so:

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

Solution 2 - Mongodb

You can use this also

const { ObjectId } = require('mongodb');
const _id = ObjectId("4eb6e7e7e9b7f4194e000001");

it's simplest way to do it

Solution 3 - Mongodb

You can do it like this:

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

EDIT: New standard has fromHexString rather than fromString

Solution 4 - Mongodb

Judging from the comments, you are looking for:

mongoose.mongo.BSONPure.ObjectID.isValid

Or

mongoose.Types.ObjectId.isValid

Solution 5 - Mongodb

var mongoose = require('mongoose');
var _id = mongoose.mongo.ObjectId("4eb6e7e7e9b7f4194e000001");

Solution 6 - Mongodb

I couldn't resolve this method (admittedly I didn't search for long)

mongoose.mongo.BSONPure.ObjectID.fromHexString

If your schema expects the property to be of type ObjectId, the conversion is implicit, at least this seems to be the case in 4.7.8.

You could use something like this however, which gives a bit more flex:

function toObjectId(ids) {

    if (ids.constructor === Array) {
        return ids.map(mongoose.Types.ObjectId);
    }

    return mongoose.Types.ObjectId(ids);
}

Solution 7 - Mongodb

Just see the below code snippet if you are implementing a REST API through express and mongoose. (Example for ADD)

....
exports.AddSomething = (req,res,next) =>{
  const newSomething = new SomeEntity({
 _id:new mongoose.Types.ObjectId(), //its very own ID
  somethingName:req.body.somethingName,
  theForeignKey: mongoose.Types.ObjectId(req.body.theForeignKey)// if you want to pass an object ID
})
}

...

Hope it Helps

Solution 8 - Mongodb

If you want to use schema

const yourSchemma = new Schema({
"customerId": {
    type: mongoose.Types.ObjectId,
    required: true
}

});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJRPeteView Question on Stackoverflow
Solution 1 - MongodbKevin DenteView Answer on Stackoverflow
Solution 2 - MongodbAsh18View Answer on Stackoverflow
Solution 3 - MongodbtalentedmrjonesView Answer on Stackoverflow
Solution 4 - MongodbA TView Answer on Stackoverflow
Solution 5 - MongodbsteampoweredView Answer on Stackoverflow
Solution 6 - MongodbBilly Jake O'ConnorView Answer on Stackoverflow
Solution 7 - MongodbManoj KumarView Answer on Stackoverflow
Solution 8 - MongodbVanderson AndradeView Answer on Stackoverflow