
- 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
How to get distinct list of sub-document field values in MongoDB?
To get distinct list of sub-document field values, you can use dot(.). The syntax is as follows −
db.yourCollectionName.distinct("yourOuterFieldName.yourInnerFieldName");
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... { ... "StudentId": 101, ... "StudentPersonalDetails": [ ... { ... "StudentName": "John", ... "StudentAge":24 ... }, ... { ... "StudentName": "Carol", ... "StudentAge":21 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c90a9abb74d7cfe6392d7d8") } > db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... ... { ... "StudentId": 102, ... "StudentPersonalDetails": [ ... { ... "StudentName": "Carol", ... "StudentAge":26 ... }, ... { ... "StudentName": "Bob", ... "StudentAge":21 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c90a9ceb74d7cfe6392d7d9") } > db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... { ... "StudentId": 103, ... "StudentPersonalDetails": [ ... { ... "StudentName": "Bob", ... "StudentAge":25 ... }, ... { ... "StudentName": "David", ... "StudentAge":24 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c90a9e6b74d7cfe6392d7da") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.getDistinctListOfSubDocumentFieldDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c90a9abb74d7cfe6392d7d8"), "StudentId" : 101, "StudentPersonalDetails" : [ { "StudentName" : "John", "StudentAge" : 24 }, { "StudentName" : "Carol", "StudentAge" : 21 } ] } { "_id" : ObjectId("5c90a9ceb74d7cfe6392d7d9"), "StudentId" : 102, "StudentPersonalDetails" : [ { "StudentName" : "Carol", "StudentAge" : 26 }, { "StudentName" : "Bob", "StudentAge" : 21 } ] } { "_id" : ObjectId("5c90a9e6b74d7cfe6392d7da"), "StudentId" : 103, "StudentPersonalDetails" : [ { "StudentName" : "Bob", "StudentAge" : 25 }, { "StudentName" : "David", "StudentAge" : 24 } ] }
Here is the query to get the distinct list of sub-document field values −
> db.getDistinctListOfSubDocumentFieldDemo.distinct("StudentPersonalDetails.StudentName");
The following is the output −
[ "Carol", "John", "Bob", "David" ]
- Related Articles
- How to sum every field in a sub document of MongoDB?
- How to add an extra field in a sub document in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?
- Get distinct levels of array field in MongoDB?
- How to subtract values (TotalPrice – Discount) from document field values in MongoDB?
- How to count number of distinct values per field/ key in MongoDB?
- Get distinct record values in MongoDB?
- How to get max values for distinct elements in MongoDB
- MongoDB query to get only distinct values
- Filter sub documents by sub document in MongoDB?
- Find all the non-distinct values of a field in MongoDB?
- Get Distinct Values with Sorted Data in MongoDB?
- Get distinct values from a column in MongoDB?
- MongoDB query to get distinct FirstName values from documents
- List all values of a certain field in MongoDB?

Advertisements