0

How do I create a mongoose schema that has the following structure

 {
       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"
     }
}
Dave Kalu
  • 1,520
  • 3
  • 19
  • 38

2 Answers2

1

This can simply be done with Arrays of Objects instead of creating New Schemas. I don't know if there might be some implications for the optimization.

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

This is following the official Mongoose documentation.

CyberMessiah
  • 1,084
  • 11
  • 14
0

You can try this

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
});
Ashraful Islam
  • 1,228
  • 10
  • 13