
- 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
MongoDB query to get only distinct values
To get distinct values, use distinct() in MongoDB. It finds the distinct values for a specified field across a single collection or view and returns the results in an array.
Let us create a collection with documents −
> db.demo287.insertOne({"details":{"AllVowels":["a","u","u","o","e","a","o","i"]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c014cf49383b52759cbbd") }
Display all documents from a collection with the help of find() method −
> db.demo287.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e4c014cf49383b52759cbbd"), "details" : { "AllVowels" : [ "a", "u", "u", "o", "e", "a", "o", "i" ] } }
Following is the query to get distinct values −
> db.demo287.distinct("details.AllVowels");
This will produce the following output −
[ "a", "e", "i", "o", "u" ]
- Related Articles
- MongoDB query to get distinct FirstName values from documents
- Get distinct record values in MongoDB?
- Get Distinct Values with Sorted Data in MongoDB?
- Get distinct values from a column in MongoDB?
- How to get max values for distinct elements in MongoDB
- MongoDB query for counting the distinct values across all documents?
- Get only the FALSE value with MongoDB query
- MongoDB query to get only a specific number of elements
- MongoDB query to select distinct and count?
- MongoDB query to get only specific fields in nested array documents?
- How to get only values in arrays with MongoDB aggregate?
- How to get distinct list of sub-document field values in MongoDB?
- Get the length of distinct values in an array with MongoDB
- MongoDB query to return only embedded document?
- MongoDB query to update only certain fields?

Advertisements