title: MongoDB query with conditional group by statement
body: <p>I need to export  customer records from database of mongoDB. Exported customer records should not have duplicated values. "firstName+lastName+code" is the key to DE-duped the record and If there are two records present in database with same key then I need to give preference to source field with value other than email.</p>
  
  <p>customer (<code>id,firstName,lastName,code,source</code>) collection is this.</p>
  
  <p>If there are record 3 records with same unique key and 3 different sources then i need to choose only one record between 2 sources(TV,internet){or if there are n number of sources i need the one record only}not with the 'email'(as email will be choosen when only one record is present with the unique key and source is email)
  query using:</p>
  
  <pre><code>db.customer.aggregate([
      {
          "$match": {
              "active": true,
              "dealerCode": { "$in": ["111391"] },
              "source": { "$in": ["email", "TV", "internet"] }
          }
      },
      {
          $group: {
              "_id": {
                  "firstName": "$personalInfo.firstName",
                  "lastName": "$personalInfo.lastName",
                  "code": "$vehicle.code"
              },
              "source": {
                  $addToSet: { "source": "$source" }
              }
          }
      },
      {
          $redact:
          {
              $cond: [
                  { $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
                  "$$PRUNE",
                  "$$DESCEND"
              ]
          }
      },
      {
          $project:
          {
              "source":
              {
                  $map:
                  {
                      "input": {
                          $cond: [
                              { $eq: [{ $size: "$source" }, 0] },
                              [{ "source": "email" }],
                              "$source"
                          ]
                      },
                      "as": "inp",
                      "in": "$$inp.source"
                  }
              },
              "record": { "_id": 1 }
          }
      }
  ])
  </code></pre>
  
  <p>sample output:</p>
  
  <pre><code>{ "_id" : { "firstName" : "sGI6YaJ36WRfI4xuJQzI7A==", "lastName" : "99eQ7i+uTOqO8X+IPW+NOA==", "code" : "1GTHK23688F113955" }, "source" : ["internet"] }
  { "_id" : { "firstName" : "WYDROTF/9vs9O7XhdIKd5Q==", "lastName" : "BM18Uq/ltcbdx0UJOXh7Sw==", "code" : "1G4GE5GV5AF180133" }, "source" : ["internet"] }
  { "_id" : { "firstName" : "id+U2gYNHQaNQRWXpe34MA==", "lastName" : "AIs1G33QnH9RB0nupJEvjw==", "code" : "1G4GE5EV0AF177966" }, "source" : ["internet"] }
  { "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "petb0Qx3YPfebSioY0wL9w==", "code" : "1G1AL55F277253143" }, "source" : ["TV"] }
  { "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "6LB/NmhbfqTagbOnHFGoog==", "code" : "1GCVKREC0EZ168134" }, "source" : ["TV", "internet"] }
  </code></pre>
  
  <p>This is a problem with this query please suggest :(</p>