
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Find data for specific date in MongoDB?
Let’s say you have saved the Login date of users. Now, you want the count of records for specific date only i.e. login date. For this, use $gte and $lt operator along with count(). Let us first create a collection with documents −
> db.findDataByDateDemo.insertOne({"UserName":"John","UserLoginDate":new ISODate("2019-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8cd7bf3115999ed511ed") } > db.findDataByDateDemo.insertOne({"UserName":"Larry","UserLoginDate":new ISODate("2019-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8ce7bf3115999ed511ee") } > db.findDataByDateDemo.insertOne({"UserName":"Sam","UserLoginDate":new ISODate("2019-05-02")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8cf3bf3115999ed511ef") } > db.findDataByDateDemo.insertOne({"UserName":"David","UserLoginDate":new ISODate("2019-05-16")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8d00bf3115999ed511f0") } > db.findDataByDateDemo.insertOne({"UserName":"Carol","UserLoginDate":new ISODate("2019-10-19")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8d0ebf3115999ed511f1") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findDataByDateDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cdd8cd7bf3115999ed511ed"), "UserName" : "John", "UserLoginDate" : ISODate("2019-01-31T00:00:00Z") } { "_id" : ObjectId("5cdd8ce7bf3115999ed511ee"), "UserName" : "Larry", "UserLoginDate" : ISODate("2019-02-01T00:00:00Z") } { "_id" : ObjectId("5cdd8cf3bf3115999ed511ef"), "UserName" : "Sam", "UserLoginDate" : ISODate("2019-05-02T00:00:00Z") } { "_id" : ObjectId("5cdd8d00bf3115999ed511f0"), "UserName" : "David", "UserLoginDate" : ISODate("2019-05-16T00:00:00Z") } { "_id" : ObjectId("5cdd8d0ebf3115999ed511f1"), "UserName" : "Carol", "UserLoginDate" : ISODate("2019-10-19T00:00:00Z") }
Following is the query to find data for a specific date in MongoDB. Here, we are getting the users who logged in between specific dates −
> db.findDataByDateDemo.count({"UserLoginDate":{ "$gte": new Date("2019-05-02"), "$lt": new Date("2019-05-18") }});
This will produce the following output −
2
- Related Articles
- Find records on or after a specific date in MongoDB?
- MongoDB query to get specific month|year (not date)?
- MongoDB function to return a specific data/value?
- How to get specific data in different formats with MongoDB?
- How to filter a query on specific date format with MongoDB?
- Insert to specific index for MongoDB array?
- MongoDB query for specific case insensitive search
- Find all collections in MongoDB with specific field?
- Find documents that contains specific field in MongoDB?
- How to convert from string to date data type in MongoDB?
- Find MongoDB documents that contains specific field?
- Find value above a specific value in MongoDB documents?
- Restrict returned data in MongoDB to display only specific values from documents
- Convert date parts to date in MongoDB
- Find which MongoDB document contains a specific string?

Advertisements