Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent _id from being populated by just one schema

So I have the following schema:

var List = new Schema(
    {
        item_details_template: {
            default: [
                {
                    "title": "Name",
                    "content": "",
                    "hidden": false,
                    "order": 0,
                    "the_type": "text"
                },
                {
                    "title": "Price",
                    "content": "",
                    "hidden": false,
                    "order": 1,
                    "the_type": "text"
                },
                {
                    "title": "Company",
                    "content": "",
                    "hidden": false,
                    "order": 2,
                    "the_type": "text"
                }
            ],
            type: [Item_Detail]
        }
    }
)

However, I don't want ONLY this schema (subdocument) to not create _id fields. How do I do this? I know you can change the original schema itself, but it's being used by other Schemas, where I would like the _id to be populated.

like image 539
A. L Avatar asked Aug 29 '18 07:08

A. L


1 Answers

To suppress _id on the item_detail_template, you need to restructure the way you create subdocument as follows

var mongoose = require("mongoose");

var subSchema = mongoose.Schema({
    //your subschema content
},{ _id : false });

var schema = mongoose.Schema({
    // schema content
    subSchemaCollection : [subSchema] // item_details_template
});

var model = mongoose.model('tablename', schema);
like image 170
Bharathvaj Ganesan Avatar answered Oct 05 '22 17:10

Bharathvaj Ganesan