Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb multiple match conditions and return documents with common name

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"}]}
}
like image 225
makhthum syed Avatar asked Mar 01 '18 10:03

makhthum syed


People also ask

What is $$ in MongoDB?

Variables can hold any BSON type data. To access the value of the variable, prefix the variable name with double dollar signs ( $$ ); i.e. "$$<variable>" . If the variable references an object, to access a specific field in the object, use the dot notation; i.e. "$$<variable>.

What is the use of $match in MongoDB?

Definition. Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

What is $EXPR in MongoDB?

$expr can build query expressions that compare fields from the same document in a $match stage. If the $match stage is part of a $lookup stage, $expr can compare fields using let variables. See Perform Multiple Joins and a Correlated Subquery with $lookup for an example.


2 Answers

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"}
    ]
 }
}])
like image 96
s7vr Avatar answered Sep 20 '22 03:09

s7vr


I resolved it using some additional count to documents.

Thanks all for your time and suggestions!

db.examSheet.aggregate([
 {
  "$match":{
    "$and":[
      {"$or":[
        {"exam":"halfyr_T","marks.p":{"$gte":"25"}},
        {"exam":"annual_T","marks.p":{"$gte":"35"}}
      ]},
      {"std":"9"},
      {"year":"2017"}
    ]
 }},
 {$group: {
        "_id": {
            "code": "$name",
            "type": { "$cond": [ 
                { "$and":[
                    { "$gte": [ "$marks.p", 25 ] },
                    { "$eq": [ "$exam", "halfyr_T" ] }
                ]},
                "A",
                "B"
            ]}
        },
        "all_data" : {$addToSet : "$$ROOT"}
    }},
        // Simply add up the results for each "type"
    { "$group": {
        "_id": "$_id.code",
        "all_data" : {$addToSet : "$all_data"},
        "score": { "$sum": 1 }
    }},
        // Now filter to keep only results with score 2
    { "$match": { "score": 2 }},
    {$project : {_id :0 , all_data : 1}}
]);
like image 45
makhthum syed Avatar answered Sep 20 '22 03:09

makhthum syed