- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return only unique values (no duplicates) in MongoDB?
You can use distinct() to return only unique values. The syntax is as follows −
db.yourCollectionName.distinct("yourFieldName");
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.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Larry","CustomerAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed7262f684a30fbdfd580") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Mike","CustomerAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed72d2f684a30fbdfd581") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Sam","CustomerAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed7322f684a30fbdfd582") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Carol","CustomerAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed73a2f684a30fbdfd583") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"David","CustomerAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed74a2f684a30fbdfd584") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Chris","CustomerAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed7582f684a30fbdfd585") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.returnOnlyUniqueValuesDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ed7262f684a30fbdfd580"), "CusomerName" : "Larry", "CustomerAge" : 23 } { "_id" : ObjectId("5c8ed72d2f684a30fbdfd581"), "CusomerName" : "Mike", "CustomerAge" : 21 } { "_id" : ObjectId("5c8ed7322f684a30fbdfd582"), "CusomerName" : "Sam", "CustomerAge" : 21 } { "_id" : ObjectId("5c8ed73a2f684a30fbdfd583"), "CusomerName" : "Carol", "CustomerAge" : 25 } { "_id" : ObjectId("5c8ed74a2f684a30fbdfd584"), "CusomerName" : "David", "CustomerAge" : 22 } { "_id" : ObjectId("5c8ed7582f684a30fbdfd585"), "CusomerName" : "Chris", "CustomerAge" : 23 }
Here is the query to return only unique values −
> db.returnOnlyUniqueValuesDemo.distinct("CustomerAge");
The following is the output:
[ 23, 21, 25, 22 ]
- Related Articles
- How can I display only unique record from MongoDB and ignore the duplicates?
- How to get unique values from MongoDB collection?
- MySQL query to return the count of only NO values from corresponding column value
- How to return only a single property “_id” in MongoDB?
- How to return only value of a field in MongoDB?
- MongoDB query to return only embedded document?
- How to get only values in arrays with MongoDB aggregate?
- How to order return duplicate column values only once in MySQL?
- Python Pandas - Return unique values in the index
- MongoDB query to get only distinct values
- How to remove duplicates from MongoDB Collection?
- Specify return format in MongoDB to return the values as an array?
- Only insert if a value is unique in MongoDB else update
- Getting unique values within two arrays in one MongoDB document
- How can we get only unique values of a column in MySQL result set?

Advertisements