
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to get the last N records in MongoDB?
- MySql how to display the records with latest ID in a table?
- 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 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?
- Finding matching records with LIKE in MongoDB?
- MongoDB query to find records with keys containing dots?
- Looping MongoDB collection for sorting
- MongoDB Aggregate to limit the number of records
- How to install the latest PowerShell module version?
- Sorting field value (FirstName) for MongoDB?
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- MongoDB query to insert but limit the total records
Advertisements