The Wayback Machine - https://web.archive.org/web/20240914091047/https://www.geeksforgeeks.org/mongodb-current-date-operator-currentdate/
Open In App

MongoDB – Current Date Operator ($currentDate)

Last Updated : 10 May, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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).

  • The default type of $currentDate operator is a Date.
  • This operator can also work with embedded/nested documents or arrays.
  • You can use this operator in methods like update(), updateOne(), etc., according to your requirements.
  • If the specified field is not found then this operator will add that field in the document.

Syntax:

{ $currentDate: { <field1>: <typeSpecification1>, ... } }

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 $currentDate operator:

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




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 .




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.




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




Previous Article
Next Article

Similar Reads

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass
Pre-requisite: AWS and MongoDB In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass.  If you haven't installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and
4 min read
MongoDB - Delete Multiple Documents Using MongoDB Shell
The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter. The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method
2 min read
Connect MongoDB Atlas Cluster to MongoDB Compass
MongoDB Compass is a free GUI for MongoDB. You might want to connect MongoDB Atlas Cluster to MongoDB Compass to take benefit of the GUI model for database administration. This guide explains how to connect MongoDB Atlas Cluster to MongoDB Compass. The connection process involves obtaining a connection string from MongoDB Atlas, configuring MongoDB
4 min read
MongoDB Shell vs MongoDB Node.JS Driver
MongoDB Shell and the MongoDB Node.js Driver are both tools for interacting with MongoDB, but they serve different purposes and are used in different contexts. Here's a comparison of the two: MongoDB Node.JS DriverA Node.js package called the MongoDB Node.js Driver enables programmatic communication between applications and MongoDB databases. Throu
4 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database known for flexibility and scalability. MongoDB Compass offers a graphical interface for managing databases visually while MongoDB Atlas is a cloud-based service for automated management and scaling of MongoDB databases. In this article, We will learn about the in-depth understanding of the difference between the
5 min read
MongoDB AND operator ( $and )
MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array. You can use this operator in methods like find(), update(), etc. ac
2 min read
MongoDB NOT operator ( $not )
MongoDB provides different types of logical query operators and $not operator is one of them. This operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression. It also includes those documents that don't contain the field. You can
2 min read
MongoDB OR operator ( $or )
MongoDB provides different types of logical query operators and $or operator is one of them. This operator is used to perform logical OR operation on the array of two or more expressions and select or retrieve only those documents that match at least one of the given expression in the array. You can use this operator in methods like find(), update(
2 min read
MongoDB NOR operator ( $nor )
MongoDB provides different types of logical query operators and $nor operator is one of them. This operator is used to perform logical NOR operation on the array of one or more expressions and select or retrieve only those documents that do not match all the given expression in the array. You can use this operator in methods like find(), update(),
2 min read
MongoDB $ln Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $ln operator is one of them. This operator is used to find the natural logarithm of a number and returns the result as a double. Syntax: { $ln: &lt;number&gt; } Here, the number is a valid expression until it resolves to a non- n
1 min read
MongoDB $sqrt Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages $sqrt operator is one of them. This operator is used to find the square root of a positive number and returns the result as a double. Syntax: { $sqrt: &lt;number&gt; } Here, the number is a valid expression until it resolves to a non
2 min read
MongoDB $mod Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $mod operator is one of them. This operator is used to divide one number by another number and return the remainder. Syntax: { $mod: [ &lt;expression1&gt;, &lt;expression2&gt; ] } Here, in this operator, the arguments passed in a
1 min read
MongoDB - $ne Operator
MongoDB $ne or "not equals" operator is one of the comparison operators. The $ne operator selects those documents where the field value is not equal to the given value. It also includes those documents that do not contain the specified field. You can use this operator in methods like find(), update(), etc. as per your requirement. Syntax{field: {$n
2 min read
MongoDB - $nin Operator
MongoDB $nin or " not in" is one of the comparison query operators. The $nin operator selects those documents where the field value is not equal to any of the given values in the array and the field that does not exist. You can use this operator in methods like find(), update(), etc. according to your requirements. Syntax{field: {$nin: [value1, val
2 min read
MongoDB - $max Operator
MongoDB $max or maximum operator is one of the field update operators. $max operator updates the field with the specified value if the specified value is greater than the current value. Important Points This operator will compare the values of different data types according to the BSON comparison order.You can also use this operator in embedded/nes
3 min read
MongoDB - $min operator
MongoDB $min or minimum operator is one of the field update operators. $min operator updates the field with the specified value if the specified value is less than the current value. The $min operator will compare the values of different data types according to the BSON comparison order. This operator can also be used in embedded/nested documents u
3 min read
MongoDB - $inc Operator
MongoDB $inc or increment operator is a type of field update operator. $inc operator increases the values of the fields to the specified amount or increases the field by the given value. Important PointsThis operator accepts positive and negative values.If the given field does not exist, then this operator will create a field and set the value of t
2 min read
MongoDB - $lt Operator
MongoDB $lt operator or less than operator is one of the comparison operators. The $lt operator selects the documents with a value less than (&lt;) the given value. You can use this operator in methods like, find(), update(), etc. as per your requirements. Syntax{field: {$lt: value}}MongoDB $lt Operator ExampleIn the following examples, we are work
2 min read
MongoDB - $lte Operator
MongoDB $lte Operator is one of the comparison operators. $lte operator selects those documents where the field value is less than equal to (&lt;=) the given value. This operator can be used in methods like find(), update(), etc. according to your requirements.. Syntax{field: {$lte: value}}MongoDB $lte Operator ExampleIn the following examples, we
2 min read
How to use the $lookup operator in aggregation pipelines in MongoDB?
One of the essential stages of the Aggregation Pipeline is the $lookup. It enables us to accomplish a left outer join between two collections. This step is quite beneficial when we have to compose data from different collections into a single document. The Aggregation Pipeline in MongoDB is a robust tool that allows us to manipulate and analyze dat
4 min read
MongoDB $gt Operator
MongoDB $gt operator selects documents where the value of a field is greater than a specified value. $gt Operator in MongoDBThe $gt operator in MongoDB is a comparison operator. It is used to find documents where the value in the field is greater than(&gt;) a given value. This operator can be used with other methods like find, update, etc. accordin
2 min read
MongoDB - $gte Operator
MongoDB $gte or "greater than equals to" operator is one of the comparison operators. $gte operator selects those documents where the field value is greater than equals to(&gt;=) the given value. This operator can be used in methods (like, find(), update(), etc.) according to your requirements. Syntax{field: {$gte: value}}MongoDB $gte Operator Exam
2 min read
MongoDB - $eq Operator
MongoDB $eq operator or equality operator is a comparison operator. The $eq operator matches documents where the value of the field is equal to the specified value. Important Points: If the given value is a document, then the order of the fields in the document is important.If the given value is an array, then MongoDB matches the documents where th
2 min read
MongoDB - $concat Operator
The $concat operator in MongoDB is a useful tool for string manipulation within aggregation pipelines. It allows us to concatenate strings or expressions to create new fields or modify existing ones. In this article, We will learn about the $concat operator in MongoDB by understanding various examples in detail. MongoDB - $concat OperatorThe $conca
3 min read
MongoDB $subtract Operator
MongoDB $subtract operator is a powerful tool in the aggregation pipeline that allows users to perform subtraction operations on numbers or dates. In this article, We will learn about MongoDB $subtract Operator by understanding various examples in detail. MongoDB $subtract OperatorMongoDB provides different types of arithmetic expression operators
3 min read
MongoDB $addToSet Operator
The $addToSet operator in MongoDB allows us to add elements to an array field if they are not already present and ensure that the array contains unique values. In this article, We will learn about the MongoDB $addToSet Operator by understanding various examples in detail. $addToSet Operator in MongoDBThe $addToSet operator in MongoDB is used to add
3 min read
MongoDB - Rename Operator ($rename)
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure. This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in
4 min read
MongoDB $arrayElemAt Operator
MongoDB $arrayElemAt operator is a key component of the aggregation framework, allowing users to retrieve specific elements from arrays within their documents. In this article, We will learn about the MongoDB $arrayElemAt by understanding various examples in detail and so on. MongoDB $arrayElemAtMongoDB provides different types of array expression
3 min read
MongoDB - $setOnInsert Operator
The $setOnInsert operator in MongoDB is a powerful tool used in updating operations with the upsert option. It allows us to specify values that should be set only when a new document is inserted. In this article, we will learn about the $setOnInsert Operator in MongoDB in detail and so on. MongoDB $setOnInsert OperatorMongoDB $setOnInsert Operator
4 min read
MongoDB - Positional Operator ($)
MongoDB provides a variety of array update operators to modify the values within array fields in documents. One such operator is the positional operator $. This operator allows us to update an element in an array without explicitly specifying the position of that element. In this article, We will learn about the Positional Operator($) in MongoDB in
4 min read