防止_id仅由一个模式填充

问题描述 投票:8回答:3

所以我有以下架构:

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

mongodb mongoose
3个回答
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);

0
投票

如果你想仅在_id模式中的item_detail_template上抑制List

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

0
投票
var widgetSchema = new Schema({ ... attributes ... }, { versionKey: false });
© www.soinside.com 2019 - 2024. All rights reserved.