Disabling ID field generation for Mongoose alternate declaration syntax for embedded documents

Mongoose 3 supports declaring an embedded document schema directly in the parent object, without declaration a separate schema object. This is described as the “Alternate declaration syntax” in the documentation here:

http://mongoosejs.com/docs/subdocs.html

with an example given:

var parentSchema = new Schema({
  children: [{ name: 'string' }]
})

I’d like to use this form, but disable the autogenerated ID property of the embedded object. Is there a way to do this? The documentation only describes how to disable it when you’re defining a separate schema instance.

Add Comment
2 Answer(s)

The closest you can get is to create the embedded schema inline:

var parentSchema = new Schema({
  children: [Schema({name: String}, {_id: false})]
});
Answered on July 11, 2016.
Add Comment

When defining a schema you can specify options as a second parameter. Set _id to false to disable auto _id.

var parentSchema = new Schema({
children: String
}, {
_id: false
})

Refer Docs : http://mongoosejs.com/docs/guide.html#_id

Answered on July 11, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.