Reputation: 3144
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")
.
Upvotes: 257
Views: 378748
Reputation: 592
I am providing a chunk of code which is working. I am using mongoose( version ^7.4.0), typescript and node.js( version 16.16.0). I use Object
const sSchema = new Schema({ "Cell Phone": String, "Work Phone": String });
const servicesModel = mongoose.model('Services', sSchema);
const objectId = Object("63aed3cc10149ed38f1a24c0");
servicesModel.find({ "_id" : objectId}).then(async (locationService) => {
console.log(locationService);
});
Upvotes: 0
Reputation: 2926
For new versions, adding 'new' keyword to the selected answer worked, like this
var mongoose = require("mongoose");
var _id = new mongoose.Types.ObjectId("64b0ee2c189286a5abc6b4ba");
Upvotes: 17
Reputation: 441
You can do it like this:
import mongoose from "mongoose";
const { ObjectId } = mongoose.Types;
const id = ObjectId('4edd40c86762e0fb12000003');
Upvotes: 3
Reputation: 141
If you want to use ObjectId a lot and don`t want to use mongoose.types.ObjectId, you can destructure your declaration:
const {
Types: { ObjectId: ObjectId },
} = require("mongoose");
const id=ObjectId("4edd40c86762e0fb12000003")
Upvotes: 0
Reputation: 362
If you want to use schema
const yourSchemma = new Schema({
"customerId": {
type: mongoose.Types.ObjectId,
required: true
}
});
Upvotes: 1
Reputation: 319
You can use this also
const { ObjectId } = require('mongodb');
const _id = ObjectId("4eb6e7e7e9b7f4194e000001");
it's simplest way to do it
Upvotes: 29
Reputation: 581
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
Upvotes: 4
Reputation: 982
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);
}
Upvotes: 6
Reputation: 12062
var mongoose = require('mongoose');
var _id = mongoose.mongo.ObjectId("4eb6e7e7e9b7f4194e000001");
Upvotes: 8
Reputation: 8101
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
Upvotes: 14
Reputation: 13826
Judging from the comments, you are looking for:
mongoose.mongo.BSONPure.ObjectID.isValid
Or
mongoose.Types.ObjectId.isValid
Upvotes: 9
Reputation: 25738
You can do it like so:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
Upvotes: 574