MongoDB query to collectively match intersection of documents against a field

To collectively match intersection of documents against a field in MongoDB, use the aggregation framework with $match, $group, and $all operators to find documents that share common field values and contain specific criteria.

Syntax

db.collection.aggregate([
    { $match: { "fieldName": "value" } },
    { $group: {
        "_id": "$groupField",
        "docs": { $push: "$$ROOT" },
        "count": { $sum: 1 }
    }},
    { $match: {
        "count": { $gt: 1 },
        "docs": {
            $all: [
                { $elemMatch: { "field": "value1" } },
                { $elemMatch: { "field": "value2" } }
            ]
        }
    }}
])

Sample Data

db.demo393.insertMany([
    {
        "Id1": "1",
        "Name": "Chris",
        "Id2": "100"
    },
    {
        "Id1": "1",
        "Name": "Chris",
        "Id2": "101"
    },
    {
        "Id1": "3",
        "Name": "Chris",
        "Id2": "100"
    },
    {
        "Id1": "3",
        "Name": "Mike",
        "Id2": "101"
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e5e6dd522064be7ab44e804"),
        ObjectId("5e5e6dd522064be7ab44e805"),
        ObjectId("5e5e6dd522064be7ab44e806"),
        ObjectId("5e5e6dd522064be7ab44e807")
    ]
}

Display all documents from the collection ?

db.demo393.find();
{ "_id": ObjectId("5e5e6dd522064be7ab44e804"), "Id1": "1", "Name": "Chris", "Id2": "100" }
{ "_id": ObjectId("5e5e6dd522064be7ab44e805"), "Id1": "1", "Name": "Chris", "Id2": "101" }
{ "_id": ObjectId("5e5e6dd522064be7ab44e806"), "Id1": "3", "Name": "Chris", "Id2": "100" }
{ "_id": ObjectId("5e5e6dd522064be7ab44e807"), "Id1": "3", "Name": "Mike", "Id2": "101" }

Example: Find Chris Documents with Both Id2 Values

Find all Id1 groups where Chris has both Id2 "100" and "101" ?

db.demo393.aggregate([
    { "$match": { "Name": "Chris" } },
    { "$group": {
        "_id": "$Id1",
        "docs": { "$push": "$$ROOT" },
        "count": { "$sum": 1 }
    }},
    { "$match": {
        "count": { "$gt": 1 },
        "docs": {
            "$all": [
                { "$elemMatch": { "Id2": "100" } },
                { "$elemMatch": { "Id2": "101" } }
            ]
        }
    }}
]);
{
    "_id": "1",
    "docs": [
        { "_id": ObjectId("5e5e6dd522064be7ab44e804"), "Id1": "1", "Name": "Chris", "Id2": "100" },
        { "_id": ObjectId("5e5e6dd522064be7ab44e805"), "Id1": "1", "Name": "Chris", "Id2": "101" }
    ],
    "count": 2
}

How It Works

  • Stage 1: $match filters documents where Name is "Chris"
  • Stage 2: $group groups by Id1, collects documents, and counts them
  • Stage 3: $match filters groups with count > 1 and both required Id2 values
  • $all ensures the group contains documents matching ALL specified conditions

Conclusion

Use aggregation with $group and $all with $elemMatch to find document intersections. This approach efficiently identifies groups that collectively satisfy multiple field criteria.

Updated on: 2026-03-15T02:47:43+05:30

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements