 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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 gather unique array item?
To gather a unique array items, use distinct(). Let us create a collection with documents −
> db.demo588.insertOne({"CountryName":["US","AUS","UK","US","UK","AUS"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e92bbd2fd2d90c177b5bccb") } Display all documents from a collection with the help of find() method −
> db.demo588.find().pretty();
This will produce the following output −
{    "_id" : ObjectId("5e92bbd2fd2d90c177b5bccb"),    "CountryName" : [       "US",       "AUS",       "UK",       "US",       "UK",       "AUS"    ] } Following is the query to gather unique array item −
> db.demo588.distinct("CountryName"); This will produce the following output −
[ "AUS", "UK", "US" ]
Advertisements
 