title: Identify documents in mongodb when matching two key:value pairs within a single array
body: <p>I am trying to identify documents where both key-value pairs within an array match using the <code>aggregate</code> pipeline.  Specifically, if I want to find documents where one array contains <code>user_attribute.Name = Quests_In_Progress</code> and <code>user_attribute.Value =3</code>.  Below is an example of such a document that I'm trying to match.</p>
  
  <p>If I use</p>
  
  <pre><code>db.myCollection.aggregate({
      $match: {
      "user_attribute.Name": "Quests_In_Progress",
      "user_attribute.Value": "3"
      }
  })
  </code></pre>
  
  <p>It will match every document that contains <code>Quests_In_Progress</code> for user_attribute.Name in one element of the array and contains "3" for user_attribute.Value, regardless of whether they exist in the same element of the array or not.<br>
  i.e. </p>
  
  <pre><code>db.myCollection.aggregate({
      $match: {
      "user_attribute.Name": "Quests_In_Progress",
      "user_attribute.Value": "0"
      }
  })
  </code></pre>
  
  <p>will match the same document simply because one element of the array has a key:Value pair of <code>Value:0</code> and another element of the array contains a key:value pair of <code>Quests_In_Progress</code>.</p>
  
  <p>What I want to do is identify documents where both of those conditions are met within one element of the array.</p>
  
  <p>I tried to do this with <code>$elemMatch</code>, but I couldn't get it to work.  Plus the aggregate documentation doesn't indicate that <code>$elemMatch</code> works, so maybe that's why I couldn't get it to work.</p>
  
  <p>Lastly, I need to use the aggregate pipeline, because there are a bunch of other things I have to do after finding these documents- specifically unwinding them.</p>
  
  <pre><code>{
      "_id": ObjectId("5555bb32de938ce667f78ce00"),
      "user_attribute": [{
      "Value": "Facebook",
      "Name": "Social_Connection"
      }, {
      "Name": "Total_Fireteam_Missions_Initiated",
      "Value": "0"
      }, {
      "Name": "Quests_Completed",
      "Value": "3"
      }, {
      "Name": "Item_Slots_Owned",
      "Value": "36"
      }, {
      "Name": "Quests_In_Progress",
      "Value": "3"
      }, {
      "Name": "Player_Progression",
      "Value": "0"
      }, {
      "Value": "1",
      "Name": "Characters_Owned"
      }, {
      "Name": "Quests_Started",
      "Value": "6"
      }, {
      "Name": "Total_Friends",
      "Value": "0"
      }, {
      "Name": "Device_Type",
      "Value": "Phone"
      }]
  }
  </code></pre>