Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
