

- 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
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 Questions & Answers
- 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?
- Get only the FALSE value with MongoDB query
- How to get max values for distinct elements in MongoDB
- MongoDB query for counting the distinct values across all documents?
- MongoDB query to get only a specific number of elements
- MongoDB query to select distinct and count?
- How to get only values in arrays with MongoDB aggregate?
- MongoDB query to get only specific fields in nested array documents?
- 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