The $currentDate is an update operator so you will need to use one of the MongoDB Date() and Datetime methods. Here is an example a few you can choose:
If it is a document with only a few fields, you could do an upsert with the $currentDate operator so that the timestamp is determined at the MongoDB server:
It is important not to match another document with the filter (first parameter); otherwise, this document would receive a new timestamp and no new document would be created.
2
Answers
The
$currentDate
is an update operator so you will need to use one of the MongoDB Date() and Datetime methods. Here is an example a few you can choose:See HERE for a full working example.
If it is a document with only a few fields, you could do an upsert with the
$currentDate
operator so that the timestamp is determined at the MongoDB server:It is important not to match another document with the filter (first parameter); otherwise, this document would receive a new timestamp and no new document would be created.
See this mongoplayground.