How to update list of dicts?

I have some records like:
{ '_id': 1, 'test_field': [{'key1': 'value1'}, {'key2': 'value2'}] }
test_field is a list of dicts. I need to push new dict in that list if any key does not exist and if it does I need to update that key’s value.

Example:

  1. {'key1': 'test_value'}'test_field': [{'key1': 'test_value'}, {'key2': 'value2'}]
  2. {'test_key': 'test_value2'}'test_field': [{'key1': 'value1'}, {'key2': 'value2'}, {'test_key': 'test_value_2'}]

Hello @Nikita_Bylnov, welcome to the MongoDB Community forum!

You can do this update using the Updates with Aggregation Pipeline feature. Below is similar post with a solution.:

4 Likes