
- 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 record values in MongoDB?
You can use distinct() method in MongoDB to get distinct record values. The syntax is as follows −
db.yourCollectionName.distinct(“yourFieldName”);
To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows −
> db.distinctRecordDemo.insertOne({"StudentId":1,"StudentName":"John","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a78299b97a65744c1b50") } > db.distinctRecordDemo.insertOne({"StudentId":2,"StudentName":"John","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a78b99b97a65744c1b51") } > db.distinctRecordDemo.insertOne({"StudentId":3,"StudentName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a79a99b97a65744c1b52") } > db.distinctRecordDemo.insertOne({"StudentId":4,"StudentName":"Carol","StudentAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a7a499b97a65744c1b53") } > db.distinctRecordDemo.insertOne({"StudentId":5,"StudentName":"Sam","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a7b499b97a65744c1b54") } > db.distinctRecordDemo.insertOne({"StudentId":6,"StudentName":"Mike","StudentAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a7c799b97a65744c1b55") } > db.distinctRecordDemo.insertOne({"StudentId":7,"StudentName":"Sam","StudentAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a7d399b97a65744c1b56") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.distinctRecordDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c77a78299b97a65744c1b50"), "StudentId" : 1, "StudentName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5c77a78b99b97a65744c1b51"), "StudentId" : 2, "StudentName" : "John", "StudentAge" : 22 } { "_id" : ObjectId("5c77a79a99b97a65744c1b52"), "StudentId" : 3, "StudentName" : "Carol", "StudentAge" : 21 } { "_id" : ObjectId("5c77a7a499b97a65744c1b53"), "StudentId" : 4, "StudentName" : "Carol", "StudentAge" : 26 } { "_id" : ObjectId("5c77a7b499b97a65744c1b54"), "StudentId" : 5, "StudentName" : "Sam", "StudentAge" : 24 } { "_id" : ObjectId("5c77a7c799b97a65744c1b55"), "StudentId" : 6, "StudentName" : "Mike", "StudentAge" : 27 } { "_id" : ObjectId("5c77a7d399b97a65744c1b56"), "StudentId" : 7, "StudentName" : "Sam", "StudentAge" : 28 }
Here is the query to get distinct records values in MongoDB.
Case 1 − Here the field is “StudentName”.
The query is as follows −
> db.distinctRecordDemo.distinct("StudentName");
The following is the output displaying distinct record for StudentName −
[ "John", "Carol", "Sam", "Mike" ]
Case 2 − Here the field is “StudentAge”.
The query is as follows −
> db.distinctRecordDemo.distinct("StudentAge");
The following is the output displaying distinct record for StudentAge −
[ 21, 22, 26, 24, 27, 28 ]
- Related Articles
- Get Distinct Values with Sorted Data in 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 the length of distinct values in an array with MongoDB
- Get Random record from MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- Getting distinct values from object array in MongoDB?
- Get distinct levels of array field in MongoDB?
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- Get distinct values and count them in MySQL
- Get distinct first words in a string with MongoDB?
- Get distinct pair of objects with all subdocuments in MongoDB?
- Find all the non-distinct values of a field in MongoDB?

Advertisements