15

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

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

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

谢谢。

4

1 回答 1

26

那个架构很好。在数组模式元素中定义一个对象被隐式地视为它自己的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})],
});
于 2013-05-14T01:45:55.683 回答