
- 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
How to get latest set of data from a MongoDB collection based on the date records?
To get the latest set of data from data records, use sort() and -1. For only a single data i.e. document, use LIMIT(1). Let us create a collection with documents −
> db.demo521.insertOne({"PurchaseDate":new ISODate("2019-01-10"),"ProductName":"Product-1"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1acb3fbf26334ef6117") } > db.demo521.insertOne({"PurchaseDate":new ISODate("2020-04-05"),"ProductName":"Product-10"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1b9b3fbf26334ef6118") } > db.demo521.insertOne({"PurchaseDate":new ISODate("2010-05-08"),"ProductName":"Product-4"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1c8b3fbf26334ef6119") } > db.demo521.insertOne({"PurchaseDate":new ISODate("2020-02-21"),"ProductName":"Product-3"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1d7b3fbf26334ef611a") }
Display all documents from a collection with the help of find() method −
> db.demo521.find();
This will produce the following output −
{ "_id" : ObjectId("5e89a1acb3fbf26334ef6117"), "PurchaseDate" : ISODate("2019-01-10T00:00:00Z"), "ProductName" : "Product-1" } { "_id" : ObjectId("5e89a1b9b3fbf26334ef6118"), "PurchaseDate" : ISODate("2020-04-05T00:00:00Z"), "ProductName" : "Product-10" } { "_id" : ObjectId("5e89a1c8b3fbf26334ef6119"), "PurchaseDate" : ISODate("2010-05-08T00:00:00Z"), "ProductName" : "Product-4" } { "_id" : ObjectId("5e89a1d7b3fbf26334ef611a"), "PurchaseDate" : ISODate("2020-02-21T00:00:00Z"), "ProductName" : "Product-3" }
Following is the query to get the latest set of data based on the date −
> db.demo521.find().sort({"PurchaseDate": -1}).limit(1);
This will produce the following output −
{ "_id" : ObjectId("5e89a1b9b3fbf26334ef6118"), "PurchaseDate" : ISODate("2020-04-05T00:00:00Z"), "ProductName" : "Product-10" }
- Related Articles
- MySQL query to fetch the latest date from a table with date records
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- Get the maximum mark records from a collection with documents in MongoDB
- Get the maximum mark records from a collection with documents in MongoDB query
- How to get array from a MongoDB collection?
- Find MongoDB records based on a condition?
- MongoDB query to get date records in a range
- Retrieve data from a MongoDB collection?
- How to get unique values from MongoDB collection?
- Set server status to inactive in a MongoDB collection with server records?
- How to continuously publish the latest N records with sorting using MongoDB?
- Set max on MongoDB capped collection?
- MongoDB query to remove entire data from a collection
- Return query based on date in MongoDB?
- Get the top most document from a MongoDB collection

Advertisements