title: Use of $COND and $EQ with a array of objects
body: <p>I'm hoping that someone might be able to answer whether what I'm trying to accomplish below can be done with the MongoDB Aggregation Framework.</p>
  
  <p>I have a user data structure that resembles the following with close to 1 million documents.</p>
  
  <pre><code>{
      "firstName" : "John",
      "lastName" : "Doe",
      "state" : "NJ",
      "email" : "JOHNDOE@XYZ.COM"
      "source" : [ 
          {
              "type" : "SOURCE-A",
              "data" : {
                  "info" : "abc",
                  "info2" : "xyz"
              }
          }, 
          {
              "type" : "SOURCE-B",
              "data" : {
                  "info3" : "abc"
              }
          }
      ]
  }
  </code></pre>
  
  <p>For the purposes of feeding data to another system, I need to generate a flat file structure with limited information from the previous dataset.  The columns need to represent:</p>
  
  <pre><code>firstname, lastname, email, is_source-a, is_source-b
  </code></pre>
  
  <p>The part that I'm having difficulty with is the conditional code that attempts to populate "is_source-a" and "is_source-b".  I have tried to use the following aggregation query, but can't figure out how to get it working since the $EQ operator used along with $COND doesn't seem to evaluate data inside of an array (always false).</p>
  
  <pre><code>db.collection.aggregate([
      {
          $project : {
              _id : 0,
              firstName : 1,
              lastName: 1,
              "is_source-a" : {
                  $cond : [
                      { $eq: [ "$source.type", "source-a" ] },
                      1,
                      0
                  ]
              },
              "is_source-b" : {
                  $cond : [
                      { $eq: [ "$source.type", "source-b" ] },
                      1,
                      0
                  ]
              }
          }
      }
  ]);
  </code></pre>
  
  <p>I could $UNWIND the array first, but then I wind up with a multiple records for each user document and don't understand how to consolidate them back.</p>
  
  <p>Is there something that I'm missing with how to use $EQ (or some other operator) along with $COND when dealing with arrays of objects?</p>