MongoDB Current Date Operator ($currentDate) (original) (raw)

Last Updated : 8 Jan, 2025

MongoDB provides different types of field update operators to update the values of the fields of the documents and

****$currentDate**

operator is one of them. This operator is used to set the value of a field to the current date (either as a timestamp or as a Date).

**Syntax:

{ $currentDate: { : , ... } }

Now, here, in this method,

_typeSpecification1

is either a boolean true to set the value of a field to the current date as a Date or a document

{ $type: "timestamp" }

or

{ $type: "date" }

which explicitly specifies the type, here the operator is case-sensitive only accept lowercase "timestamp" or "date". To specify

_field

in embedded/nested documents with the help of dot notation. In the following examples, we are working with:

**Database: GeeksforGeeks **Collection: Employee **Document: three documents that contain the details of the employees in the form of field-value pairs.

Update the date field using$currentDateoperator:

In the example, we are updating the value of joiningDate field of an employee’s document whose first name is Om.

Python `

db.Employee.updateOne({"name.first": "Om"}, {$currentDate: {joiningDate: true}})

`

Update the date field in the embedded document using $currentDate operator:

In the example, we are updating the value of joiningDate field of an employee’s document whose first name is .

Python `

db.Employee.updateOne({"name.first": "Sumit"}, {$currentDate: {"personalDetails.joiningDate": true}})

`

Adding date field using $currentDate operator:

In the example, we are adding a new date field whose value is assigned by the $cuurentDate operator in the employee’s document whose first name is Amu.

Python `

db.Employee.updateOne({"name.first": "Amu"}, {$currentDate: {joiningDate: {$type: "date"}}})

`