Performing distinct on multiple fields in MongoDB?

To perform distinct on multiple fields in MongoDB, use the $group operator with the aggregation framework. The $addToSet operator collects unique values for each field across all documents.

Syntax

db.collection.aggregate([
    {
        $group: {
            _id: null,
            field1: { $addToSet: "$field1" },
            field2: { $addToSet: "$field2" },
            field3: { $addToSet: "$field3" }
        }
    }
]);

Sample Data

db.distinctOnMultipleFieldsDemo.insertMany([
    {
        "StudentFirstName": "Chris",
        "StudentAge": 21,
        "StudentCountryName": "US"
    },
    {
        "StudentFirstName": "Robert",
        "StudentAge": 21,
        "StudentCountryName": "AUS"
    },
    {
        "StudentFirstName": "Chris",
        "StudentAge": 22,
        "StudentCountryName": "UK"
    },
    {
        "StudentFirstName": "David",
        "StudentAge": 24,
        "StudentCountryName": "AUS"
    }
]);
{
    "acknowledged": true,
    "insertedIds": {
        "0": ObjectId("5cd2e518b64f4b851c3a13d7"),
        "1": ObjectId("5cd2e518b64f4b851c3a13d8"),
        "2": ObjectId("5cd2e518b64f4b851c3a13d9"),
        "3": ObjectId("5cd2e518b64f4b851c3a13da")
    }
}

Verify Sample Data

db.distinctOnMultipleFieldsDemo.find().pretty();
{
    "_id": ObjectId("5cd2e518b64f4b851c3a13d7"),
    "StudentFirstName": "Chris",
    "StudentAge": 21,
    "StudentCountryName": "US"
}
{
    "_id": ObjectId("5cd2e518b64f4b851c3a13d8"),
    "StudentFirstName": "Robert",
    "StudentAge": 21,
    "StudentCountryName": "AUS"
}
{
    "_id": ObjectId("5cd2e518b64f4b851c3a13d9"),
    "StudentFirstName": "Chris",
    "StudentAge": 22,
    "StudentCountryName": "UK"
}
{
    "_id": ObjectId("5cd2e518b64f4b851c3a13da"),
    "StudentFirstName": "David",
    "StudentAge": 24,
    "StudentCountryName": "AUS"
}

Example: Distinct Values Across Multiple Fields

Get distinct values for StudentFirstName, StudentAge, and StudentCountryName ?

db.distinctOnMultipleFieldsDemo.aggregate([
    {
        $group: {
            _id: 0,
            StudentFirstName: { $addToSet: "$StudentFirstName" },
            StudentAge: { $addToSet: "$StudentAge" },
            StudentCountryName: { $addToSet: "$StudentCountryName" }
        }
    }
]);
{
    "_id": 0,
    "StudentFirstName": ["David", "Robert", "Chris"],
    "StudentAge": [24, 22, 21],
    "StudentCountryName": ["UK", "AUS", "US"]
}

How It Works

  • $group groups all documents together using _id: 0
  • $addToSet collects unique values for each specified field
  • Duplicate values are automatically removed from the result arrays

Conclusion

Use $group with $addToSet to get distinct values across multiple fields in a single aggregation query. This approach efficiently collects unique values from all specified fields simultaneously.

Updated on: 2026-03-15T01:05:24+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements