title: Mongodb .aggregate not matching $or?
body: <p>Whether a user sends or receives a message, he/she should be able to archive the message for him/herself.</p>
  
  <p>This .aggregate $or, however, only returns docs that the user is the recipient of ('to') that he/she has marked 'archive':'true'. It does accurately filter messages according to the 'value' Booleans.</p>
  
  <pre><code>Models.Messages.aggregate([
  
      // Match documents
      { "$match": {
        "$or" : [
            {
                "to": { 
                      "$elemMatch": {
                          "username": 'userA',
                          "view.archive": true,
                          "view.bin": false
                      }
                }
            },
            {
                "from" : {
                    "$elemMatch" : {
                        "username" : 'userA',
                        "view.archive": true,
                        "view.bin": false
                    }
                }
            }
        ],
        "$or": [
                 { 'value.1': true },
                 { 'value.2': true },
                 { 'value.3': true },
                 { 'value.4': false } 
               ]
      }},
  
      // Unwind to de-normalize
      { "$unwind": "$to" },
      { "$unwind": "$from" },
  
      // Match the array elements      
      { "$match": {
         "to.username": 'userA',
         "to.view.archive": true,
         "from.username": 'userA',
         "from.view.archive": true
      }},
  
      // Group back all the elements of the original document
      { "$group": {
         "_id": "$_id",
         "from": { "$push": "$from" },
         "to": { "$push": "$to" },
         "message": { "$first": "$message" },
         "updated": { "$first": "$updated" }
      }},
  
      // Sort by updated, most recent first (descending)
      {"$sort": {"updated": -1}}
  
  ], function (err, docs) {
  </code></pre>
  
  <p>How could this be altered so that all the fields of both of the following messages are aggregated for userA:</p>
  
  <pre><code>{
      "_id" : ObjectId("53e83867f316ea7f22fd3b2b"),
      "updated" : ISODate("2014-08-11T03:29:06.000Z"),
      "message" : "message1",
      "value" : [
                  "1" : true,
                  "2" : false,
                  "3" : false,
                  "4" : false
              ]
      "to" : [ 
          {
              "user" : ObjectId("53e835bd76e0d04318d8cc4e"),
              "username" : "userA",
              "_id" : ObjectId("53e83867f316ea7f22fd3b2c"),
              "view" : {
                  "inbox" : false,
                  "outbox" : false,
                  "archive" : true,
                  "bin" : false
              }
          }
      ],
      "from" : [ 
          {
              "user" : ObjectId("53e8360276e0d04318d8cc55"),
              "username" : "userB",
              "_id" : ObjectId("53e83867f316ea7f22fd3b2d"),
              "view" : {
                  "inbox" : false,
                  "outbox" : true,
                  "archive" : false,
                  "bin" : false
              }
          }
      ],
      "__v" : 5
  }
  {
      "_id" : ObjectId("53e83867f316ea7f22fd3b2b"),
      "updated" : ISODate("2014-08-11T03:29:06.000Z"),
      "message" : "message2",
      "value" : [
                  "1" : false,
                  "2" : true,
                  "3" : false,
                  "4" : false
              ]
      "to" : [ 
          {
              "user" : ObjectId("53e8360276e0d04318d8cc55"),
              "username" : "userB",
              "_id" : ObjectId("53e83867f316ea7f22fd3b2c"),
              "view" : {
                  "inbox" : true,
                  "outbox" : false,
                  "archive" : false,
                  "bin" : false
              }
          }
      ],
      "from" : [ 
          {
              "user" : ObjectId("53e835bd76e0d04318d8cc4e"),
              "username" : "userA",
              "_id" : ObjectId("53e83867f316ea7f22fd3b2d"),
              "view" : {
                  "inbox" : false,
                  "outbox" : true,
                  "archive" : true,
                  "bin" : false
              }
          }
      ],
      "__v" : 5
  }
  </code></pre>