
- 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 continuously publish the latest N records with sorting using MongoDB?
To publish the latest N records with sorting, use sort() along with limit(). Here, set the number of records you want to show with limit(). Let us create a collection with documents −
> db.demo454.insertOne({"ClientName":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e7cce8cdbcb9adb296c95c0") } > db.demo454.insertOne({"ClientName":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e7cce95dbcb9adb296c95c1") } > db.demo454.insertOne({"ClientName":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e7cce9fdbcb9adb296c95c2") } > db.demo454.insertOne({"ClientName":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e7ccea6dbcb9adb296c95c3") } > db.demo454.insertOne({"ClientName":"Mike"});{ "acknowledged" : true, "insertedId" : ObjectId("5e7cceafdbcb9adb296c95c4") }
Display all documents from a collection with the help of find() method −
> db.demo454.find();
This will produce the following output −
{ "_id" : ObjectId("5e7cce8cdbcb9adb296c95c0"), "ClientName" : "Chris" } { "_id" : ObjectId("5e7cce95dbcb9adb296c95c1"), "ClientName" : "John" } { "_id" : ObjectId("5e7cce9fdbcb9adb296c95c2"), "ClientName" : "Bob" } { "_id" : ObjectId("5e7ccea6dbcb9adb296c95c3"), "ClientName" : "David" } { "_id" : ObjectId("5e7cceafdbcb9adb296c95c4"), "ClientName" : "Mike" }
Following is the query to publish the latest N records using SORT() and LIMIT() in MongoDB −
> db.demo454.find().sort({ClientName:-1}).limit(3);
This will produce the following output −
{ "_id" : ObjectId("5e7cceafdbcb9adb296c95c4"), "ClientName" : "Mike" } { "_id" : ObjectId("5e7cce95dbcb9adb296c95c1"), "ClientName" : "John" } { "_id" : ObjectId("5e7ccea6dbcb9adb296c95c3"), "ClientName" : "David" }
- Related Articles
- MySql how to display the records with latest ID in a table?
- How to get the last N records in MongoDB?
- How to get latest set of data from a MongoDB collection based on the date records?
- MySQL query to fetch the latest date from a table with date records
- How to Publish a YouTube Video using Mobile YouTube App
- How to convert birth date records to age with MongoDB
- Using aggregation pipeline to fetch records in MongoDB
- How to find latest entries in array over all MongoDB documents?
- MongoDB query to find records with keys containing dots?
- Finding matching records with LIKE in MongoDB?
- How to run MongoDB shell using mongos command?\n
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- MongoDB query to search date records using only Month and Day
- Looping MongoDB collection for sorting
- MongoDB Aggregate to limit the number of records

Advertisements