Mongoose: Custom ObjectIds - How to?

Created on 6 Aug 2011  路  11Comments  路  Source: Automattic/mongoose

Hi all,
this is more a question than an issue (mongoose newbie) but could someone point me to an example of how to use custom ObectIds with mongoose?
whenever I properly define, i.e. _id: mongoose.Schema.ObjectId in a schema and then try to create the ObjectId with a custom key, i.e. myModel._id = new ObjectId('unique'), I get an invalid ObjectId error.

Using Strings as _id and automatic ObjectIds generated by mongoose works well, but I really need custom (and unique) keys for the ObjectIds within my models and want to use the DBRef plugin to make proper references in mongodb between collections.

Any hints?
Thanks in advance for your help!

Most helpful comment

i want to have a custom _id and i define it in schema . but when i use "findOneAndUpdate" and set "upsert" and "setDefaultsOnInsert" true for it ,in creating my document my custom _id is skipped and default ObjectId set for it

All 11 comments

I also have this issue, How can I create an ObjectId out of a string?

ObjectIds are specd here: http://www.mongodb.org/display/DOCS/Object+IDs

What you want is something else. If you don't want the default optimized ObjectId you can override it in your schema:

new Schema({ _id: String })

@guiomie to create an ObjectId from a string:

var ObjectId = require('mongoose').Types.ObjectId;
var myObjectId = ObjectId.fromString('myhexstring');

Why do I get:

TypeError: Object function ObjectId() {
throw new Error('This is an abstract interface. Its only purpose is to mark '
+ 'fields as ObjectId in the schema creation.');
} has no method 'fromString'

Thank you

@guiomie you're using mongoose.Schema.ObjectId instead of mongoose.Types.ObjectId

I know get a Error: Invalid ObjectId ... any ideas?

I have the same problem:

var ObjectId = mongoose.Types.ObjectId;
console.log("Converting to ObjectId from [%s]", objId);
return ObjectId.fromString(objId);

This gives an error:

Converting to ObjectId from [4eed2d88c3dedf0d0300001c]
Error: Invalid ObjectId
    at Function.fromString (/home/olo/.node_libraries/.npm/mongoose/2.3.13/package/lib/drivers/node-mongodb-native/objectid.js:27:11)

objId is probably not a string.

On Sat, Dec 17, 2011 at 9:55 PM, Aleksander Adamowski <
[email protected]

wrote:

I have the same problem:

var ObjectId = mongoose.Types.ObjectId;
console.log("Converting to ObjectId from [%s]", objId);
return ObjectId.fromString(objId);

This gives an error:

Converting to ObjectId from [4eed2d88c3dedf0d0300001c]
Error: Invalid ObjectId
   at Function.fromString
(/home/olo/.node_libraries/.npm/mongoose/2.3.13/package/lib/drivers/node-mongodb-native/objectid.js:27:11)

Reply to this email directly or view it on GitHub:
https://github.com/LearnBoost/mongoose/issues/447#issuecomment-3192945

Aaron

You were right :)

Thanks!

I am trying to get an object id from timestamp field to use in a query.Can someone help,i tried the solution above

i want to have a custom _id and i define it in schema . but when i use "findOneAndUpdate" and set "upsert" and "setDefaultsOnInsert" true for it ,in creating my document my custom _id is skipped and default ObjectId set for it

@aliabdzad what did you do about that issue
did setting something like
new Schema({ _id: { type: String, default: _ => uuid() } })
work

Was this page helpful?
0 / 5 - 0 ratings