node.js 了解猫鼬 [Schema.Types.Mixed]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16533439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:29:45  来源:igfitidea点击:

understanding mongoose [Schema.Types.Mixed]

node.jsmongodbschemamongoose

提问by cathy.sasaki

Is the below schema defined correctly or does writingneed to be writing: [Schema.Types.Mixed]orwriting: [{}]?

以下架构是否正确定义或确实writing需要是 writing: [Schema.Types.Mixed]writing: [{}]

That is, if you have an array of dictionaries -- [{},{},{}] -- one can't predefine the internal structure unless you create another schema and embed it. Is that the right interpretation of the docs?

也就是说,如果您有一系列字典 -- [{},{},{}] -- 除非您创建另一个架构并将其嵌入,否则无法预定义内部结构。这是对文档的正确解释吗?

http://mongoosejs.com/docs/schematypes.html

http://mongoosejs.com/docs/schematypes.html

var blogSchema = new mongoose.Schema({
  title:  String,
  writing: [{
        post: String,
        two: Number,
        three : Number,
        four  : String,
        five : [{  a: String,
                    b : String,
                    c  : String,
                    d: String,
                    e: { type: Date, default: Date.now }, 
                }]
  }],
});

Thanks.

谢谢。

回答by JohnnyHK

That schema is fine. Defining an object within an array schema element is implicitly treated as its own Schemaobject. As such they'll have their own _idfield, but you can disable that by explicitly defining the schema with the _idoption disabled:

那个架构很好。在数组架构元素中定义一个对象被隐式地视为它自己的Schema对象。因此,他们将拥有自己的_id字段,但您可以通过使用_id禁用选项显式定义架构来禁用它:

var blogSchema = new mongoose.Schema({
    title: String,
    writing: [new Schema({
        post: String,
        two: Number,
        three : Number,
        four  : String,
        five : [new Schema({ 
            a: String,
            b: String,
            c: String,
            d: String,
            e: { type: Date, default: Date.now }, 
        }, {_id: false})]
    }, {_id: false})],
});