mongodb multiple match conditions and return documents with common name

makhthum syed picture makhthum syed · Mar 1, 2018 · Viewed 22.7k times · Source

below data is present in "examSheet" collection

{"name":"a1", "std":"9", "year":"2017", "exam":"halfyr_T", "marks":[{"p":"45","m":"40","c":"50"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"halfyr_P", "marks":[{"p":"40","m":"28","c":"38"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"annual_T", "marks":[{"p":"40","m":"50","c":"48"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"annual_P", "marks":[{"p":"45","m":"42","c":"18"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"halfyr_T", "marks":[{"p":"25","m":"30","c":"50"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"halfyr_P", "marks":[{"p":"41","m":"48","c":"28"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"annual_T", "marks":[{"p":"30","m":"48","c":"24"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"annual_P", "marks":[{"p":"35","m":"08","c":"38"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"halfyr_T","marks":[{"p":"45","m":"40","c":"50"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"halfyr_P", "marks": [{"p":"40","m":"28","c":"38"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"annual_T", "marks": [{"p":"40","m":"50","c":"48"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"annual_P", "marks": [{"p":"45","m":"42","c":"18"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"halfyr_T", "marks": [{"p":"25","m":"30","c":"50"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"halfyr_P", "marks": [{"p":"41","m":"48","c":"28"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"annual_T", "marks": [{"p":"30","m":"48","c":"24"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"annual_P", "marks": [{"p":"35","m":"08","c":"38"}]}

...

i want to return aggregated json output, where a name satisfies all the conditions. Ex: std:9 , year:2017, exam:halfyr_Theory with physics marks > 25 and std:9 , year:2017, exam: annual_Theory with physics marks > 35

I tried different ways like below, matching with conditions, able to get 'name', but unable to match again/ extract the documents data.

db.examSheet.aggregate([{$facet: {
    'halfyr': [ {$match: {$and: [{'exam': 'halfyr_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '25'}}]}}],
    "annual": [ {$match: {$and: [{'exam': 'annual_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '35'}}]}}] 
    }
},
{$project: {'_id': 0, "combined": {$setIntersection: ['$halfyr.name', '$annual.name']}}}
                    ]);

Tried, matching halfyr.name with $in [combined] after project, etc. but unable to solve the issue.

Please help or suggest me resolving this.

I tried this way.

db.examSheet.aggregate([{$facet: {
                            "halfyr": [ {$match: {$and: [{'exam': 'halfyr_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '25'}}]}}],
                            "annual": [ {$match: {$and: [{'exam': 'annual_T'},{'std': '9'},{'year': "2017"},{'marks.p': {$gte: '35'}}]}}] 
                            }
                    },
                    {$unwind : "$annual"}
                ]);

My output is:

{
    "halfyr" : [
            {
                    "_id" : ObjectId("5a98c639b0ae80e6c6a92031"),
                    "name" : "a1",
                    "std" : "9",
                    "year" : "2017",
                    "exam" : "halfyr_T",
                    "marks" : [
                            {
                                    "p" : "45",
                                    "m" : "40",
                                    "c" : "50"
                            }
                    ]
            },
            {
                    "_id" : ObjectId("5a98c639b0ae80e6c6a9203e"),
                    "name" : "a2",
                    "std" : "9",
                    "year" : "2017",
                    "exam" : "halfyr_T",
                    "marks" : [
                            {
                                    "p" : "25",
                                    "m" : "30",
                                    "c" : "50"
                            }
                    ]
            }
    ],
    "annual" : {
            "_id" : ObjectId("5a98c639b0ae80e6c6a92038"),
            "name" : "a1",
            "std" : "9",
            "year" : "2017",
            "exam" : "annual_T",
            "marks" : [
                    {
                            "p" : "40",
                            "m" : "50",
                            "c" : "48"
                    }
            ]
    }

}

Can anyone suggest how to match or filter out where names are common in halfyr and annual? Thanks in advance!!

Desired output:

{
"name":"a1", "std":"9", "year":"2017",
"halfyr" : {"exam":"halfyr_T", "marks": [{"p":"45","m":"40","c":"50"}]},
"annual" : {"exam":"annual_T", "marks": [{"p":"40","m":"50","c":"48"}]}
}

Answer

s7vr picture s7vr · Mar 1, 2018

You can use $or. inside the $and.

db.examSheet.aggregate([
 {
  "$match":{
    "$and":[
      {"$or":[
        {"exam":"halfyr_T","marks.p":{"$gte":"25"}},
        {"exam":"annual_T","marks.p":{"$gte":"35"}}
      ]},
      {"std":"9"},
      {"year":"2017"}
    ]
 }
}])