Assume a collection where each document contains an array my_array
consisting of multiple dictionaries. Each dictionary in the array contains itself an entry with key my_key
.
How can I efficiently update all documents in the collection, creating a new field my_list
per document which shall contain all my_key
values of the dictionaries in the array?
Update with an example document:
{
'_id': '123',
'irrelevant_field_1': '1',
'irrelevant_field_2': '2',
'my_array': [
{'my_key': 'a', 'other_key_1': 'b'},
{'my_key': 'aa', 'other_key_2': 'bb'},
{'my_key': 'aaa', 'other_key_3': 'bbb'}
]
}
I would like to update with a new field 'my_list': ['a', 'aa', 'aaa']
at the same level as my_array
.