$currentDate
The $currentDate operator is used to set the value of a field to the current date and time. This operator is useful for automatically updating a field with the current timestamp when a document is inserted or updated.
Parameters
-
field: The field to update with the current date and time. -
type: (optional) Specifies the BSON type to use for the current date. Can be eitherdateortimestamp.
Example (MongoDB Shell)
The following example demonstrates how to use the $currentDate operator to set the lastModified field to the current date and time when a new document is inserted.
Create sample documents
db.users.insert({ name: "John Doe", email: "john.doe@example.com" })
Query example
db.users.updateOne( { name: "John Doe" }, { $currentDate: { lastModified: true } } )
View updated document
db.users.findOne({ name: "John Doe" })
Output
{
_id: ObjectId('...'),
name: 'John Doe',
email: 'john.doe@example.com',
lastModified: ISODate('2025-10-25T22:50:29.963Z')
}
Code examples
To view a code example for using the $currentDate command, choose the tab for the language that you want to use: