You can use the following basic syntax to perform a query with a date range in MongoDB:
db.collection.find({ day: { $gt: ISODate("2020-01-21"), $lt: ISODate("2020-01-24") } })This particular query will return all documents in the collection where the “day” field is greater than 2020-01-21 and less than 2020-01-24.
Note that $gt indicates “greater than” and $lt indicates “less than.”
You can also use $gte for “greater than or equal” and $lte for “less than or equal.”
The following examples show how to use this syntax in practice with a collection sales with the following documents:
db.sales.insertOne({day: new Date("2020-01-20"), amount: 40}) db.sales.insertOne({day: new Date("2020-01-21"), amount: 32}) db.sales.insertOne({day: new Date("2020-01-22"), amount: 19}) db.sales.insertOne({day: new Date("2020-01-23"), amount: 29}) db.sales.insertOne({day: new Date("2020-01-24"), amount: 35})Example 1: Find Documents Between Two Dates
We can use the following code to find all documents where the “day” field is between two specific dates:
This query returns the following two documents:
{ _id: ObjectId("618548bc7529c93ea0b41490"), day: 2020-01-22T00:00:00.000Z, amount: 19 } { _id: ObjectId("618548bc7529c93ea0b41491"), day: 2020-01-23T00:00:00.000Z, amount: 29 }Example 2: Find Documents After Specific Date
We can use the following code to find all documents where the “day” field is after a specific date:
db.sales.find({ day: { $gt: ISODate("2020-01-22") } })This query returns the following two documents:
{ _id: ObjectId("618548bc7529c93ea0b41491"), day: 2020-01-23T00:00:00.000Z, amount: 29 } { _id: ObjectId("618548bc7529c93ea0b41492"), day: 2020-01-24T00:00:00.000Z, amount: 35 }Example 3: Find Documents Before Specific Date
We can use the following code to find all documents where the “day” field is before a specific date:
db.sales.find({ day: { $lt: ISODate("2020-01-22") } })This query returns the following two documents:
{ _id: ObjectId("618548bc7529c93ea0b4148e"), day: 2020-01-20T00:00:00.000Z, amount: 40 } { _id: ObjectId("618548bc7529c93ea0b4148f"), day: 2020-01-21T00:00:00.000Z, amount: 32 }Note: You can find the complete documentation for the ISODate() function here.
Additional Resources
The following tutorials explain how to perform other common queries in MongoDB:
MongoDB: How to Query with “Like” Regex
MongoDB: How to Check if Field Contains a String
The mongo shell provides various methods like ISODate() to return the date, either as a string or as a Date object. ISODate() constructor returns a Date object using the ISODate() wrapper.
Let us create a collection with documents −
> db.demo548.insertOne({"dueDate":new ISODate("2020-04-09 12:12:40")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e30499e5f92834d7f05de") }Display all documents from a collection with the help of find() method −
This will produce the following output −
{ "_id" : ObjectId("5e8e30499e5f92834d7f05de"), "dueDate" : ISODate("2020-04- 09T12:12:40Z") }Following is the query to display, month, day, week, year, etc. from ISODate −
> db.demo548.aggregate( [ { $project: { Year: { $year: "$dueDate" }, Month: { $month: "$dueDate" }, Day: { $dayOfMonth: "$dueDate" }, Hour: { $hour: "$dueDate" }, Minutes: { $minute: "$dueDate" }, Seconds: { $second: "$dueDate" }, Milliseconds: { $millisecond: "$dueDate" }, DayOfYear: { $dayOfYear: "$dueDate" }, DayOfWeek: { $dayOfWeek: "$dueDate" }, Week: { $week: "$dueDate" } } } ] ).pretty();This will produce the following output −
{ "_id" : ObjectId("5e8e30499e5f92834d7f05de"), "Year" : 2020, "Month" : 4, "Day" : 9, "Hour" : 12, "Minutes" : 12, "Seconds" : 40, "Milliseconds" : 0, "DayOfYear" : 100, "DayOfWeek" : 5, "Week" : 14 }
Updated on 14-May-2020 07:13:56
- Related Questions & Answers
- Finding day of week from date (day, month, year) in JavaScript
- Create date from day, month, year fields in MySQL?
- Convert day of year to day of month in Java
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- How to get a Date from year, month and day in Java?
- MongoDB query to fetch date records (ISODate format) in a range
- How can fetch records from specific month and year in a MySQL table?
- Format MySQL date and convert to year-month-day
- How can we fetch month and day from a given date in MySQL?
- MySQL query to fetch date with year and month?
- Filter the records of current day, month and year in MySQL?
- How to get current day, month and year in Java 8?
- Combine three strings (day, month, year) and calculate next date PHP?
- MySQL to fetch records based on a specific month and year?
- How to convert year, month, and day of the month into a complete date in R?