- 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
MongoDB query to 'sort' and display a specific number of values
To sort in MongoDB, use sort(). For displaying only a specific number of values, use LIMIT. Let us create a collection with documents −
> db.demo254.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a0ab1627c0c63e7dba7f") } > db.demo254.insertOne({"Name":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a0b01627c0c63e7dba80") } > db.demo254.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a0b71627c0c63e7dba81") }
Display all documents from a collection with the help of find() method −
> db.demo254.find();
This will produce the following output −
{ "_id" : ObjectId("5e47a0ab1627c0c63e7dba7f"), "Name" : "Chris" } { "_id" : ObjectId("5e47a0b01627c0c63e7dba80"), "Name" : "Adam" } { "_id" : ObjectId("5e47a0b71627c0c63e7dba81"), "Name" : "Bob" }
Following is the query to sort −
> db.demo254.find().sort({"Name":1});
This will produce the following output −
{ "_id" : ObjectId("5e47a0b01627c0c63e7dba80"), "Name" : "Adam" } { "_id" : ObjectId("5e47a0b71627c0c63e7dba81"), "Name" : "Bob" } { "_id" : ObjectId("5e47a0ab1627c0c63e7dba7f"), "Name" : "Chris" }
Following is the query to sort and display only a specific number of records with limit().
> db.demo254.find().sort({"Name":1}).limit(2);
This will produce the following output −
{ "_id" : ObjectId("5e47a0b01627c0c63e7dba80"), "Name" : "Adam" } { "_id" : ObjectId("5e47a0b71627c0c63e7dba81"), "Name" : "Bob" }
- Related Articles
- MongoDB query with an 'or' condition?
- How to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?
- MongoDB query to limit subdocuments having the given fields of the 'projection'
- MongoDB query to get a specific number of items
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?
- Is calling a female 'dear' or 'sweetie' harassment of some sort?
- Display day number with SimpleDateFormat('d') in Java
- Query Array for 'true' value at index n in MongoDB?
- MongoDB query to display documents with a specific name irrespective of case
- MySQL query to select ENUM('M', 'F') as 'Male' or 'Female'?
- Program to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
- MongoDB query to group records and display a specific value with dot notation
- MongoDB query to get only a specific number of elements
- Opposite of $addToSet to '$removeFromSet' in MongoDB?
- MongoDB query select and display only a specific field from the document?

Advertisements