![]() |
VOOZH | about |
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: { <field1>: <typeSpecification1>, ... } }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.👁 Image
$currentDate operator: In the example, we are updating the value of joiningDate field of an employee’s document whose first name is Om.
👁 Image$currentDate operator: In the example, we are updating the value of joiningDate field of an employee’s document whose first name is .
👁 Image$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.
👁 Image