
- 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
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 Articles
- Get distinct record values in MongoDB?
- Select two fields and return a sorted array with their distinct values in MongoDB?
- Get the length of distinct values in an array with MongoDB
- Get distinct values from a column in MongoDB?
- MongoDB query to get only distinct values
- How to get max values for distinct elements in MongoDB
- MongoDB query to get distinct FirstName values from documents
- Get distinct first words in 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 distinct first word from a string with MongoDB?
- Getting distinct values from object array in MongoDB?
- Get the last two values with MongoDB
- Get distinct levels of array field in MongoDB?
- Get distinct values and count them in MySQL

Advertisements