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

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


Add the auto flag:

_id: {
    type: mongoose.Schema.Types.ObjectId,
    index: true,
    required: true,
    auto: true,
  }

source


Instead of:

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

You should do:

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