【问题标题】:mongoose.js 3: how to tell that nested is not a documentmongoose.js 3:如何判断嵌套不是文档
【发布时间】:2012-09-19 00:35:53
【问题描述】:

我的猫鼬架构:

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

如何告诉猫鼬项目(和项目的项目)不是文档,而只是嵌套对象?我既不需要 _id 属性,也不需要它们的任何文档功能,但我想定义它们并使用模式进行限制。

_id: false够了吗?

【问题讨论】:

    标签: mongoose


    【解决方案1】:

    没有自己架构的嵌入式文档数组(如上所示)将始终具有_id 字段。如果你想禁止 _id 他们必须有自己的架构,你需要在他们的架构定义上设置 { _id: false } option。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2012-10-07
      • 2022-11-24
      相关资源
      最近更新 更多