title: mongodb multiple match conditions and return documents with common name
body: <p>below data is present in "examSheet" collection</p>
  
  <pre><code>{"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"}]}
  </code></pre>
  
  <p>...</p>
  
  <p>i want to return aggregated json output, where a name satisfies all the conditions.
  <em>Ex: std:9 , year:2017, exam:halfyr_Theory with physics marks > 25 and 
  std:9 , year:2017, exam: annual_Theory with physics marks > 35</em></p>
  
  <p>I tried different ways like below,
  matching with conditions,
  able to get 'name', but unable to match again/ extract the documents data.</p>
  
  <pre><code>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']}}}
                      ]);
  </code></pre>
  
  <p>Tried, matching halfyr.name with <code>$in [combined]</code> after project, etc. 
  but unable to solve the issue.</p>
  
  <p>Please help or suggest me resolving this.</p>
  
  <p>I tried this way.</p>
  
  <pre><code>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"}
                  ]);
  </code></pre>
  
  <p>My output is:</p>
  
  <pre><code>{
      "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"
                      }
              ]
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Can anyone suggest how to match or filter out where names are common in halfyr and annual?
  Thanks in advance!!</p>
  
  <p>Desired output:</p>
  
  <pre><code>{
  "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"}]}
  }
  </code></pre>