
- 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
How to efficiently perform “distinct†with multiple keys in MongoDB?
You can perform distinct with multiple keys with the help of an aggregate framework.
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.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike","StudentAge":22,"StudentMathMarks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f74488d10a061296a3c53") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike","StudentAge":22,"StudentMathMarks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f744b8d10a061296a3c54") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f74598d10a061296a3c55") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f745e8d10a061296a3c56") } > db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Carol","StudentAge":27,"StudentMathMarks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f74688d10a061296a3c57") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.distinctWithMultipleKeysDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7f74488d10a061296a3c53"), "StudentName" : "Mike", "StudentAge" : 22, "StudentMathMarks" : 56 } { "_id" : ObjectId("5c7f744b8d10a061296a3c54"), "StudentName" : "Mike", "StudentAge" : 22, "StudentMathMarks" : 56 } { "_id" : ObjectId("5c7f74598d10a061296a3c55"), "StudentName" : "Bob", "StudentAge" : 23, "StudentMathMarks" : 45 } { "_id" : ObjectId("5c7f745e8d10a061296a3c56"), "StudentName" : "Bob", "StudentAge" : 23, "StudentMathMarks" : 45 } { "_id" : ObjectId("5c7f74688d10a061296a3c57"), "StudentName" : "Carol", "StudentAge" : 27, "StudentMathMarks" : 54 }
Here is the query to perform distinct with multiple keys −
> c = db.distinctWithMultipleKeysDemo; test.distinctWithMultipleKeysDemo > myResult = c.aggregate( [ {"$group": { "_id": { StudentName:"$StudentName", StudentAge: "$StudentAge" } } } ] );
The following is the output −
{ "_id" : { "StudentName" : "Carol", "StudentAge" : 27 } } { "_id" : { "StudentName" : "Bob", "StudentAge" : 23 } } { "_id" : { "StudentName" : "Mike", "StudentAge" : 22 } }
- Related Articles
- MongoDB aggregation with multiple keys
- Performing distinct on multiple fields in MongoDB?
- Perform group and distinct together in a single MongoDB query
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- How to update array with multiple conditions in MongoDB
- How to efficiently run complex queries on MongoDB unindexed fields?
- Python dictionary with keys having multiple inputs
- MongoDB query to find records with keys containing dots?
- Get Distinct Values with Sorted Data in MongoDB?
- How to perform ascending order sort in MongoDB?
- How to perform descending order sort in MongoDB?
- Perform min/max with MongoDB aggregation
- Search a value in an object with number keys with MongoDB
- How to count number of keys in a MongoDB document?

Advertisements