Adding fields in dictionaries inside list

I want to update by pushing new fields to an existing dictionary object inside a list. For example, the current structure looks like:

[
  {
    _id: 1 ,
    name: 'example',
    info: [
      { num: '123' },
      { num: '456' },
    ],
  }
]

Say I want to add new fields like “age” and “address” specifically to the dict where num = “123”.
So, I would want something like:

info: [
      { num: '123' , age: 32, address: "xyz" },
      { num: '456' },
    ]

How would I go about this?
I’ve looked at MongoDB 4.2 update pipeline: “Push or Update” in nested array but found it difficult to understand. I would really appreciate explanations on how to do something like this. Thanks in advance.

Hello @noirblnq, welcome to the MongoDB Community forum!

You can use the following $ array update operator usage for your use case:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.