0

如何创建具有以下结构的猫鼬模式

 {
       data: {
        name: "John doe",
        attributes: [
          {
            text: "Sample text",
            created_at: "2018-08-23"
         },
        {
            text: "Sample text 2",
            created_at: "2018-08-23"
         }
        ],
       created_at: "2018-08-23"
     }
}
4

2 回答 2

1

这可以简单地通过对象数组完成,而不是创建新模式。我不知道优化是否有一些影响。

    attributes: [{
    text: String,
    created_at: Date
}], 

这是遵循官方 Mongoose 文档的。

于 2019-07-30T16:29:22.403 回答
0

你可以试试这个

const sampleSchema = new mongoose.Schema({
    data: {
        type: dataSchema
    }
});

const dataSchema = new mongoose.Schema({
    name: String,
    attributes: [attributeSchema],
    created_at: Date
});

const attributeSchema = new mongoose.Schema({
    text: String,
    created_at: Date
});
于 2018-09-16T11:15:23.273 回答