I have documents in collection which have structure:
{key: {0: 2, 1: 5, 2: 100, 4: 15}}
I need to add pair key: value to dictionary. value may be anything. It doesn’t matter. But key must be the smallest integer that is not used in the dictionary as a key (starts from 0).
I can do it using python.
document = {key: {0: 2, 1: 5, 2: 100, 4: 15}}
new_key = 0 # key to insert in our dict
for i in range(max(document['key'].keys()) + 2):
if i not in document['key'].keys():
new_key = i
# update the document
document['key'][new_key] = value # value may be anything
How to do it using MongoDB? Is it possible?