-1

Schema example:

_id: ObjectId
list: [{field: String}]

If I want to add an array of objects into list - how can I do that?
E.g. I have document like here:

{
  _id: 1
  list: [
    {field: "value1"}, {field: "value2"}
  ]
}

And I want to add into the list this array:

[{field: "val3"}, {field: "val4"}, {field: "val5"}]

Is there a way to do it?

1
  • Is there anything that you have tried? Commented Mar 11, 2015 at 7:25

2 Answers 2

2

You can use the $push operator together with $each to add all the items to an array in MongoDB:

db.collection.update({_id: 1}, { $push: { list: { $each: [{field: "val3"}, {field: "val4"}, {field: "val5"}]}}});
Sign up to request clarification or add additional context in comments.

Comments

0

To add objects into the array, you can use push function

var newFields  = [{field: "val3"}, {field: "val4"}, {field: "val5"}];
list.push(newFields);    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.