I have a document as follows:
{
"_id": "1234",
"created_at": 1678787680
}
I want to modify the document and add a new key updated_at
which will be a datetime equivalent of the created_at
UNIX timestamp.
{
"_id": "1234",
"created_at": 1678787680,
"updated_at": "2023-03-14 15:39:18.767232"
}
Is there a way to perform this operation using updateMany
?
2
Answers
Try this one:
Use
$toDate
. You are better off storing dates as proper BSON date types (not as strings in your example).mongosh:
pymongo:
full example:
prints: