I'm trying to update my collection
For the single, object need to update the status and updated time.
Input object id as id
new status as current status
Condition If the new status
is different from the status
in DB, then need to $push
new status
and timestamp
into activity_log
array.
And update the update the updated_time
and status
of the record.
If the new status
and previous status is same then updated_time
will be updated.
Is it possible to do that in pymongo using single update ?
collection.update({
'_id': ObjectId(id),
}, {
'$cond': {
'if': {
'status': {
'$ne': current_status
}
},
'then': {
'$push': {
'activity_log': {
'status': current_status,
'changed_time': datetime.now()
}
}
},
'else': None
},
'$set': {
'status': current_status,
'updated_time': datetime.now()
}
})