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 $group stage with multiple fields in the _id to get distinct combinations.
  • The _id field 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.

Updated on: 2026-03-15T00:06:05+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements