
- 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 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" ]
- Related Articles
- MongoDB query to remove item from array?
- MongoDB query to set a sub item in an array?
- Removing item from array in MongoDB?
- Get unique item from two different array in JavaScript
- How to gather extended query execution stats in Oracle?
- MongoDB query to fetch array values
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- How to add new item in nested array with MongoDB?
- MongoDB Query to implement $in in array
- Create array with MongoDB query?
- MongoDB query for array concatenation?
- MongoDB query to update array with another field?
- MongoDB query to change order of array elements?
- MongoDB query to push document into an array

Advertisements