Is there a way to create a calculated field in mongodb using pymongo?
example = {
"field1": 1,
"field2": 2,
"calculated_field": "field1" + "field2"
}
The calculated field must always keep the formula, if "field1" will later be modified, the result must update.
I have read mongodb documentation and I can see it can be done with aggregation pipeline but pymongo's documentation is not really clear on this procedure.
Edit:
I am trying, at the moment, to insert a new field as below but the field is not added.
pipeline = [
{
"$addFields": {
"calculated_field": {"$sum": ["field1", "field2"]}
}
}
]
dbCollection = database["col"]
dbCollection.aggregate(pipeline)