Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongoose

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.

like image 616
Kevin Dente Avatar asked Oct 16 '13 17:10

Kevin Dente


1 Answers

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

like image 76
SUNDARRAJAN K Avatar answered Sep 21 '22 05:09

SUNDARRAJAN K