Express 创建包含对象数组的mongoose架构

Express 创建包含对象数组的mongoose架构,express,mongoose,mongoose-schema,mongoose-models,Express,Mongoose,Mongoose Schema,Mongoose Models,如何创建具有以下结构的mongoose模式 { data: { name: "John doe", attributes: [ { text: "Sample text", created_at: "2018-08-23" }, { text: "Sample text 2", created_at:

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

 {
       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"
     }
}
你可以试试这个

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

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

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

这是官方的猫鼬文件

尝试过,但创建的_at不能反映根据您的要求更改类型。您可以使用
Date.now()
根据Mongoose文档,Mongoose有一个timestamps属性,可以自动生成时间戳。我的问题是时间戳应用于主对象,而不是对象数组。我在
dataSchema
中遗漏了一个属性。我已经更新了这个片段。现在试试这个片段。