1

I want to achieve an element in schema like that:

array: [
    {id: 0},
    {id: 1},
    {id: 2}
]

In othr words, I want to declare an array that will hold 3 objects and those 3 objects should already have ids set. I had several attempts, but the objects never showed up

1 Answer 1

3

There is not a lot of detail to your question so I am not sure what those ids are/should be but you could do this in your schema:

var YourSchema = new Schema({
  foo: {
    type: [{}],
    default: [{id: 1}, {id:2}, {id:3}]
  }
}

That would on record creation setup that array with those objects inside.

The result would be:

{
  "_id" : ObjectId("SOMEID"),
  "foo" : [ 
      {
          "id" : 1
      }, 
      {
          "id" : 2
      }, 
      {
          "id" : 3
      }
  ],
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.