防止_id仅由一种模式填充

时间:2018-08-29 07:05:40

标签: mongodb mongoose

所以我有以下模式:

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]
        }
    }
)

但是,我不想仅此架构(子文档)不创建_id字段。我该怎么做呢?我知道您可以更改原始模式本身,但是其他模式正在使用它,我希望在其中填充_id

3 个答案:

答案 0 :(得分:2)

要在_id上隐藏item_detail_template,您需要按照以下方式重新构造创建子文档的方式

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);

答案 1 :(得分:0)

如果仅想在_id模式中对item_detail_template抑制List

var List = new Schema(
    {
        item_details_template: {
            _id:false, // should supress _id property
            default: [
            ...

答案 2 :(得分:0)

var widgetSchema = new Schema({ ... attributes ... }, { versionKey: false });