

- 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
Get Distinct Values with Sorted Data in MongoDB?
Following is the query to get distinct values with sorted data in MongoDB
db.yourCollectionName.distinct("yourFieldName").sort();
Let us first create a collection with documents
>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10,"StudentName":"John","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e3315e86fd1496b38c3") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20,"StudentName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e3e15e86fd1496b38c4") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10,"StudentName":"John","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e4415e86fd1496b38c5") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":30,"StudentName":"Chris","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e5115e86fd1496b38c6") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20,"StudentName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e5715e86fd1496b38c7") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40,"StudentName":"Bob","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e6515e86fd1496b38c8") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40,"StudentName":"Bob","Stude ntAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e6d15e86fd1496b38c9") }
Following is the query to display all documents from a collection with the help of find() method;
> db.getDistinctWithSortedDataDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b1e3315e86fd1496b38c3"), "StudentId" : 10, "StudentName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5c9b1e3e15e86fd1496b38c4"), "StudentId" : 20, "StudentName" : "Carol", "StudentAge" : 21 } { "_id" : ObjectId("5c9b1e4415e86fd1496b38c5"), "StudentId" : 10, "StudentName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5c9b1e5115e86fd1496b38c6"), "StudentId" : 30, "StudentName" : "Chris", "StudentAge" : 22 } { "_id" : ObjectId("5c9b1e5715e86fd1496b38c7"), "StudentId" : 20, "StudentName" : "Carol", "StudentAge" : 21 } { "_id" : ObjectId("5c9b1e6515e86fd1496b38c8"), "StudentId" : 40, "StudentName" : "Bob", "StudentAge" : 20 } { "_id" : ObjectId("5c9b1e6d15e86fd1496b38c9"), "StudentId" : 40, "StudentName" : "Bob", "StudentAge" : 20 }
Following is the query to get distinct values with sorted data for “StudentName”
> db.getDistinctWithSortedDataDemo.distinct("StudentName").sort();
This will produce the following output
[ "Bob", "Carol", "Chris", "John" ]
Following is the query to get distinct values with sorted data for StudentAge
> db.getDistinctWithSortedDataDemo.distinct("StudentAge").sort();
This will produce the following output
[ 20, 21, 22, 23 ]
- Related Questions & Answers
- Get distinct record values in MongoDB?
- MongoDB query to get only distinct values
- Get distinct values from a column in MongoDB?
- Get the length of distinct values in an array with MongoDB
- Select two fields and return a sorted array with their distinct values in MongoDB?
- MongoDB query to get distinct FirstName values from documents
- How to get max values for distinct elements in MongoDB
- Get distinct first words in a string with MongoDB?
- Get distinct first word from a string with MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- Get distinct pair of objects with all subdocuments in MongoDB?
- Get the last two values with MongoDB
- Getting distinct values from object array in MongoDB?
- Get distinct levels of array field in MongoDB?
- Get distinct values and count them in MySQL
Advertisements