In this MongoDB tutorial, we will understand the Current date in MongoDB. We will cover this topic with different operations and examples. Furthermore, the following topics will cover:
- The current date in the MongoDB query
- Insert the current date in MongoDB
- Find a current date field in MongoDB
- How to find greater than the current date field in MongoDB
- How to find a date less than the current date field in MongoDB
- Update date to current date in MongoDB
- Add current date in MongoDB using aggregation
- Insert current date using MongoDB Compass
- Insert the current date in MongoDB using python
The current date in the MongoDB query
In this topic, you will learn to create the current date field in MongoDB. The following two ways return date as a string or as a date object:
- The Date() will return the current date as a string.
- The new Date() will return the current date a date object and MongoDB cover the Date object with the ISODate.
You can specify a particular date by passing an ISO-8601 ( represent date and time as human-readable strings in new plain-text communication protocols and file formats) date string with a year the new Date() constructor or the ISODate() function.
The function accepts the following formats:
- The new Date(“<yyyy-mm-dd>”) returns the ISODate with the specified date.
- The new Date(“YYYY-mm-ddTHH:MM:ss>”) specifies the datetime in the client’s local timezone and returns the ISOdate with the specified datetime in UTC.
Example:
The following query is used to create the current date:
var myDate = new Date();
print(myDate)
Here, the new Date() will create a current date and store it into the myDate variable. And, the print() function is used to display the date.

Read: MongoDB find multiple values
Insert the current date in MongoDB
When we are inserting some documents into the collection, sometimes there is a date field as well. In this topic, you will learn to insert the current date field into the collection.
Example:
The following query will use to insert a document into the addCurrentDate collection.
db.addCurrentDate.insertOne({"StudentName":"Biden","StudentAdmissionDate":new Date() })
Here, we have used the insertOne() to insert one document into the collection. And, the new Date() will create a current date.

We have successfully inserted the current date field into the addCurrentDate collection.
Read: MongoDB text search partial words
Find current date field in MongoDB
In this topic, you will learn to find the current date field. When you have a large dataset and you want to find out the current date field document from the collection. Furthermore, we will understand it with the help of an example.
Example:
The following documents were inserted into the collection:
db.store.insertMany([
{"_id": 1, "name": "Google Chrome", "lastModified": "Wed Oct 20 2021 17:01:08 GMT+0530 (India Standard Time)" },
{"_id": 2, "name": "UC Browser", "lastModified": "Wed Oct 20 2021 17:01:08 GMT+0530 (India Standard Time)" },
{"_id": 3, "lastModified": ISODate("2021-11-11T05:32:44.412Z") }
])
After that, we will apply the below query to the collection to find the current date field.
db.store.find({"lastModified": new ISODate("2021-11-11T05:32:44.412Z") })
Here, we have defined the current date in the parameter and returned all the documents using the find() method.

we have successfully retrieved all the document fields of the current date.
Read: MongoDB remove a field from the document
How to find greater than the current date field in MongoDB
In this topic, you will learn to find documents greater than the current date field. We will understand this with the help of an example:
Example:
The following documents were inserted into the Sales collection.
db.Sales.insertMany([
{"_id": 1, "item": "abc", "price": NumberDecimal("10"), "quantity" : 2, "date" : ISODate("2020-03-01T08:00:00Z") },
{"_id": 2, "item": "jkl", "price": NumberDecimal("20"), "quantity": 1, "date" : ISODate("2020-03-01T09:00:00Z") },
{ "_id": 3, "item": "xyz", "price": NumberDecimal("5"), "quantity": 10, "date" : ISODate("2020-03-15T09:00:00Z") },
{"_id": 4, "item": "xyz", "price": NumberDecimal("5"), "quantity": 20, "date" : ISODate("2020-04-04T11:21:39.736Z") },
{"_id": 5, "item": "abc", "price": NumberDecimal("10"), "quantity": 10, "date" : ISODate("2021-04-04T21:23:13.331Z") },
{"_id": 6, "item": "def", "price": NumberDecimal("7.5"), "quantity": 5, "date" : ISODate("2021-06-04T05:08:13Z") },
{"_id": 7, "item": "def", "price": NumberDecimal("7.5"), "quantity": 10, "date" : ISODate("2021-09-10T08:43:00Z") },
{"_id": 8, "item": "abc", "price": NumberDecimal("10"), "quantity": 5, "date" : ISODate("2021-02-06T20:20:13Z") }
])
After that, we will apply the below query and retrieve all greater than the current date field.
db.Sales.find( {"date": { $gt: new ISODate("2021-04-04T21:23:13.331Z") } } )
Here, we have applied the $gt operator and returned all the documents greater than the current date field.

We have successfully retrieved the documents where documents are greater than the current date field.
Read: MongoDB Auto Increment ID
How to find date less than the current date field in MongoDB
In this topic, you will learn to find the date field less than the current date field in MongoDB. Let get understand this with the help of an example.
Example:
In this example, we are using the same collection that we have used in the previous topic. For reference, check the below snapshot:

Now, we will apply the below query to find the date fields that are less than the current date fields.
db.Sales.find( {"date": { $lt: new ISODate("2021-04-04T21:23:13.331Z") } } )
Here, we have applied the $lt operator and returned all the documents less than the current date field.

We have successfully returned all the documents where the date field is less than the current date field.
Read: Create index in MongoDB
Update date to current date in MongoDB
In this topic, you will learn to update a date field to the current date in the documents. Let get understand this with the help of an example.
Example:
The following documents were inserted into the student collection.
db.students.insertMany([
{"_id": 1, "tests": [ 95, 92, 90 ], "lastUpdate": ISODate("2021-09-15T03:30:32.008Z"), "average": 92, "grade": "A" },
{"_id": 2, "tests": [ 94, 88, 90 ], "lastUpdate": ISODate("2021-09-15T03:30:32.008Z"), "average": 90, "grade": "A" },
{"_id": 3, "tests": [ 70, 75, 82 ], "lastUpdate": ISODate("2021-09-15T03:30:32.008Z"), "average": 75, "grade": "C" }
])
After that, we will apply the below query to update the date to the current date in the documents.
db.students.update({_id: 2}, { $set: {"lastUpdate": new Date()}} )
Here, we have used the update() method to update the document field where the $set operator replaces the value of the lastUpdate date field with the current date value using the new Date() where id is 2.

We have successfully updated the date field with the current date.
Read: MongoDB two-phase commit
Add current date in MongoDB using aggregation
In this topic, you will learn to add the current date field in the documents using the aggregation pipeline.
Example:
In this example, you will learn to add the current date field into the documents using the aggregation pipeline.
The following documents were inserted into the collection.
db.address.insertMany([
{"name": "rommel", "blk_no": 12, "street": "dewey street", "city": "United States" },
{"name": "gary", "blk_no": 15, "street": "gordon street", "city": "United Kingdom" }
])
Now, we will apply the below query to the collection and add the current date field using the aggregation pipeline.
db.address.aggregate({ $addFields: {dateField: new Date() }}).pretty()
Here, we have used the aggregate() method and use the $addFields to add new dateField (current date) to documents.

Note: In MongoDB starting version 4.2, MongoDB adds a new aggregation pipeline stage $set that is the nickname for $addFields. We can also use the $set to add new fields in the existing documents.
Read: MongoDB create and change user password
Insert current date using MongoDB Compass
In this topic, you will learn to insert the current date fields into documents using MongoDB Compass. Let get understand this with the help of an example.
Example:
You have to follow the below steps to understand the below example.
In this example, we are using the existing collection of the database to insert the current date field.
- Open the MongoDB Compass and connect to the server.
- Select the existing database and collection, aggregation and address respectively.

- Now, click on aggregation to insert current date field into documents using aggregation pipline.
- Select the $addFields stage to add a new field DateField into the existing documents.
/**
* newField: The new field name.
* expression: The new field expression.
*/
{
DateField: new Date()
}
Here, the new Date() returns the current date as a Date object. Also, check out the below snapshot for the output.

We have successfully added the current date field into the existing documents using MongoDB Compass.
Read: MongoDB find multiple conditions
Insert the current date in MongoDB using python
In this topic, you will learn to insert the current date field in the existing collection using python. Let get understand this with the help of an example.
Example:
In this example, we will use the existing collection and insert the current date field using python.
In the code, we have used the following steps:
- Import the pymongo and datetime library to use MongoDB and current date functionality in python respectively.
- Use the MongoClient class to connect with the host.
- After that, access the existing database and collection, aggregation and address respectively.
- Use the aggregate() function and use $set operator to add the current date() field.
- Here, the now() function is use to add the current date and time.
import pymongo
import datetime
# creating a MongoClient object and connect with the host
myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")
# accessing the database
mydb = myclient["aggregation"]
# accessing the collection of the database
mycol = mydb["address"]
# Insert the current date field into collection
collect = mycol.aggregate([{ "$set": { "DateField": datetime.datetime.now() }} ] )
for current_date in collect:
print(current_date)
For the output check the below snapshot:

We have successfully inserted the current date field into the documents using python.
You may also like to read the following MongoDB tutorials.
- Display MongoDB data in HTML
- Distinct query in MongoDB
- MongoDB find by ID
- MongoDB compare two fields
- MongoDB order by date
So, In this tutorial, we have understood the current date in MongoDB. Additionally, We have covered the following list of topics:
- The current date in the MongoDB query
- Insert the current date in MongoDB
- Find a current date field in MongoDB
- How to find greater than the current date field in MongoDB
- How to find a date less than the current date field in MongoDB
- Update date to current date in MongoDB
- Add current date in MongoDB using aggregation
- Insert current date using MongoDB Compass
- Insert the current date in MongoDB using python
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.