- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return query based on date in MongoDB?
To return query based on the date in MongoDB, let us take an example.
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.returnQueryFromDate.insertOne({"PassengerName":"John","PassengerAge":23,"PassengerArrivalTime":new ISODate("2018-03-10 14:45:56")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57be9064dcd4a68b70e4") } > db.returnQueryFromDate.insertOne({"PassengerName":"Larry","PassengerAge":21,"PassengerArrivalTime":new ISODate("2018-05-19 11:10:23")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e5") } > db.returnQueryFromDate.insertOne({"PassengerName":"Mike","PassengerAge":24,"PassengerArrivalTime":new ISODate("2018-08-25 16:40:12")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e6") } >db.returnQueryFromDate.insertOne({"PassengerName":"Carol","PassengerAge":26,"PassengerArrivalTime":new ISODate("2019-01-29 09:45:10")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e7") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.returnQueryFromDate.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8a57be9064dcd4a68b70e4"), "PassengerName" : "John", "PassengerAge" : 23, "PassengerArrivalTime" : ISODate("2018-03-10T14:45:56Z") } { "_id" : ObjectId("5c8a57bf9064dcd4a68b70e5"), "PassengerName" : "Larry", "PassengerAge" : 21, "PassengerArrivalTime" : ISODate("2018-05-19T11:10:23Z") } { "_id" : ObjectId("5c8a57bf9064dcd4a68b70e6"), "PassengerName" : "Mike", "PassengerAge" : 24, "PassengerArrivalTime" : ISODate("2018-08-25T16:40:12Z") } { "_id" : ObjectId("5c8a57bf9064dcd4a68b70e7"), "PassengerName" : "Carol", "PassengerAge" : 26, "PassengerArrivalTime" : ISODate("2019-01-29T09:45:10Z") }
Here is return query based on date. The records with a date greater than 2018-05-19T11:10:23Z will be displayed −
> db.returnQueryFromDate.find({"PassengerArrivalTime" : { $gte : new ISODate("2018-05-19T11:10:23Z") }}).pretty();
The following is the output −
{ "_id" : ObjectId("5c8a57bf9064dcd4a68b70e5"), "PassengerName" : "Larry", "PassengerAge" : 21, "PassengerArrivalTime" : ISODate("2018-05-19T11:10:23Z") } { "_id" : ObjectId("5c8a57bf9064dcd4a68b70e6"), "PassengerName" : "Mike", "PassengerAge" : 24, "PassengerArrivalTime" : ISODate("2018-08-25T16:40:12Z") } { "_id" : ObjectId("5c8a57bf9064dcd4a68b70e7"), "PassengerName" : "Carol", "PassengerAge" : 26, "PassengerArrivalTime" : ISODate("2019-01-29T09:45:10Z") }
- Related Articles
- MySQL query to return the entire date and time based on a string and format
- Group by day/month/week based on the date range in MongoDB
- MongoDB query to fetch only the “Name” field based on roles?
- MySQL query with two boolean conditions to extract date based on hour?
- How to filter a query on specific date format with MongoDB?
- How to get tag count in MongoDB query results based on list of names?
- MongoDB query to return only embedded document?
- How to work Date query with ISODate in MongoDB?
- MongoDB query to get date records in a range
- How to get latest set of data from a MongoDB collection based on the date records?
- Update quantity in MongoDB based on two conditions?
- Calculate age based on date of birth in MySQL?
- How to calculate a future date based on a given date in Excel?
- MongoDB query to get specific month|year (not date)?
- Find MongoDB records based on a condition?

Advertisements