Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to auto generate ObjectId when a mongoose Model is new'ed?

is there a way to declare a Model schema in mongoose so that when the model is new'ed the _id field would auto-generate?

for example:

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectIdSchema = Schema.ObjectId; var ObjectId = mongoose.Types.ObjectId;  var PersonSchema = new Schema({     _id:            ObjectIdSchema,     firstName:      {type: String, default: 'N/A'},     lastName:       {type: String, default: 'N/A'},     age:            {type: Number, min: 1} });  var Person = mongoose.model('Person', PersonSchema); 

at first, i thought great!, i'll just do

_id:    {type:ObjectIdSchema, default: new ObjectId()} 

but of course that doesn't work, because new ObjectId() is only called on initialize of schema. so calling new Persion() twice creates two objects with the same _id value.

so is there a way to do it so that every time i do "new Person()" that a new ObjectId() is generated?

the reason why i'm trying to do this is because i need to know the value of the new person's _id value for further processing.

i also tried:

var person = new Person({firstName: "Joe", lastName: "Baz"}); person.save(function(err, doc, num){     console.log(doc._id); }); 

even then, doc doesn't contain the ObjectId. but if i look in the database, it does contain it.

p.s. i'm using mongoose 2.7.1

p.p.s. i know i can manually create the ObjectId when creating the person as such:

var person = new Person({_id: new ObjectId(), firstName: "Joe", lastName: "Baz"}); 

but i rather not have to import ObjectId and have to new it every time i want to new a Person. guess i'm used to using the java driver for mongodb, where i can just create the value for the _id field in the Model constructor.

like image 550
Khon Lieu Avatar asked Jul 22 '12 23:07

Khon Lieu


People also ask

Does Mongoose auto generate ID?

_id field is auto generated by Mongoose and gets attached to the Model, and at the time of saving/inserting the document into MongoDB, MongoDB will use that unique _id field which was generated by Mongoose.

How do I get the ObjectId after I save an object in Mongoose?

To get the object ID after an object is saved in Mongoose, we can get the value from the callback that's run after we call save . const { Schema } = require('mongoose') mongoose. connect('mongodb://localhost/lol', (err) => { if (err) { console. log(err) } }); const ChatSchema = new Schema({ name: String }); mongoose.

What is Mongoose schema ObjectId?

ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose. ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.

Does Mongoose model create collection?

Mongoose never create any collection until you will save/create any document.


1 Answers

the moment you call var person = new Person(); person._id should give you the id (even if it hasn't been saved yet). Just instantiating it is enough to give it an id. You can still save it after, and that will store the id as well as the rest of the person object

like image 74
Last Rose Studios Avatar answered Sep 22 '22 01:09

Last Rose Studios