WHITECOLOR
WHITECOLOR

Reputation: 26122

mongoose.js 3: how to tell that nested is not a document

My mongoose Schema:

mongoose.Schema({
        title: 'string',
        items: [{
            uid: 'string',
            type: {type: 'string'},
            title: 'string',
            items: [{uid: 'string', type: {type: 'string'}, text: 'string'}]
        }]
    });

How to tell mongoose that items (and items of items) are not documents, but just nested objects? I need neither the _id property nor any document's functionality for them, but I want to define them and restrict with schema.

Is _id: false is enough?

Upvotes: 1

Views: 1086

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Embedded document arrays without their own schema (like you show above) will always have an _id field. If you want to suppress the _id they have to have their own schema and you need to set the { _id: false } option on their schema definition.

mongoose.Schema({
    title: 'string',
    items: [mongoose.Schema({
        uid: 'string',
        type: {type: 'string'},
        title: 'string',
        items: [mongoose.Schema({
            uid: 'string', 
            type: {type: 'string'}, 
            text: 'string'
        }, {_id: false})]
    }, {_id: false})]
});

Upvotes: 6

Related Questions