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
-
Economics & Finance
Selected Reading
How to efficiently perform "distinct" with multiple keys in MongoDB?
To efficiently perform distinct operations with multiple keys in MongoDB, use the aggregation framework with the $group stage. This groups documents by multiple fields and returns unique combinations.
Syntax
db.collection.aggregate([
{
$group: {
_id: {
field1: "$field1",
field2: "$field2",
field3: "$field3"
}
}
}
]);
Sample Data
db.distinctWithMultipleKeysDemo.insertMany([
{"StudentName": "Mike", "StudentAge": 22, "StudentMathMarks": 56},
{"StudentName": "Mike", "StudentAge": 22, "StudentMathMarks": 56},
{"StudentName": "Bob", "StudentAge": 23, "StudentMathMarks": 45},
{"StudentName": "Bob", "StudentAge": 23, "StudentMathMarks": 45},
{"StudentName": "Carol", "StudentAge": 27, "StudentMathMarks": 54}
]);
Display all documents to see the duplicates ?
db.distinctWithMultipleKeysDemo.find();
{
"_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
}
Example: Distinct by Multiple Keys
Get unique combinations of StudentName and StudentAge ?
db.distinctWithMultipleKeysDemo.aggregate([
{
$group: {
_id: {
StudentName: "$StudentName",
StudentAge: "$StudentAge"
}
}
}
]);
{ "_id": { "StudentName": "Carol", "StudentAge": 27 } }
{ "_id": { "StudentName": "Bob", "StudentAge": 23 } }
{ "_id": { "StudentName": "Mike", "StudentAge": 22 } }
Key Points
- Use
$groupstage with multiple fields in the_idto get distinct combinations. - The
_idfield automatically eliminates duplicates based on the specified field combinations. - This approach is more flexible than the basic
distinct()method which works on single fields only.
Conclusion
MongoDB's aggregation framework with $group efficiently handles distinct operations across multiple keys. This method returns unique field combinations, making it ideal for complex data analysis scenarios.
Advertisements
