
- 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
Get distinct levels of array field in MongoDB?
To get distinct levels of array field, use $addToSet in MongoDB. Let us create a collection with documents −
> db.demo122.insertOne({"ListOfValues":[100,10]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f20f1140daf4c2a3544b6") } > db.demo122.insertOne({"ListOfValues":[240,10]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f20f7140daf4c2a3544b7") }
Display all documents from a collection with the help of find() method −
> db.demo122.find();
This will produce the following output −
{ "_id" : ObjectId("5e2f20f1140daf4c2a3544b6"), "ListOfValues" : [ 100, 10 ] } { "_id" : ObjectId("5e2f20f7140daf4c2a3544b7"), "ListOfValues" : [ 240, 10 ] }
Following is the query to get distinct levels of array field in MongoDB −
> db.demo122.aggregate([ ... { ... "$group": { ... "_id": 0, ... "ListOfValues": { "$addToSet": "$ListOfValues" } ... } ... } ... ])
This will produce the following output −
{ "_id" : 0, "ListOfValues" : [ [ 240, 10 ], [ 100, 10 ] ] }
- Related Articles
- How to get distinct list of sub-document field values in MongoDB?
- Get the length of distinct values in an array with MongoDB
- Get index of given element in array field in MongoDB?
- Get distinct record values in MongoDB?
- Get count of array elements from a specific field in MongoDB documents?
- Find all the non-distinct values of a field in MongoDB?
- Reverse array field in MongoDB?
- How to count number of distinct values per field/ key in MongoDB?
- Get Distinct Values with Sorted Data in MongoDB?
- Get distinct values from a column in MongoDB?
- Get distinct pair of objects with all subdocuments in MongoDB?
- MongoDB slice array in populated field?
- MongoDB query to get only distinct values
- Getting distinct values from object array in MongoDB?
- Get the duplicate values of a field in MongoDB?

Advertisements