Expected behavior - passed dictionary remains unchanged outside function.
Hi,
This question is more related to Python. In Python if you are passing a dictionary (mutable) to a function, and the function modifies the dictionary, the original dictionary will also be modified.
In this case, _id
field was added as a reference identifier of that document within MongoDB collection. In most cases, this is a useful addition especially if there’s a requirement to find that document soon after.
Generally a way to preserve the original dictionary is to perform copy.deepcopy() on the dictionary before it’s passed to functions. However this comes with its own overhead. This may or may not be suitable for your use case.
Regards,
Wan.