title: How to $filter where condition is matched against an array?
body: <p>after $lookup i will get a join named lastViewed like this:</p>
  
  <pre><code>{
    "_id" : "5955ea4fd8099718330ab191"
  
      lastViewed: [
          {
             "_id" : ObjectId("595218a7d346d27fb0bc1705"),
             "userId" : ObjectId("58c796d4344b9da4dfbe027b"),
             "groupId" : ObjectId("5955ea4fd8099718330ab191"),
             "lastViewedTime" : ISODate("2017-06-19T09:39:07.374Z")
          },
          {
             "_id" : ObjectId("595218a7d346d27fb0bc1764"),
             "userId" : ObjectId("58c796d4344b9da4dfbe027b"),
             "groupId" : ObjectId("5955ea4fd8099718330ab162"),
            "lastViewedTime" : ISODate("2017-05-11T09:39:07.374Z")
          }
        ]
  }
  </code></pre>
  
  <p>I want to use $filter and then $project values ,
  my query is below where the userId is getting from params</p>
  
  <pre><code>$project: { "viewedDetails": {
                          "$filter": {
                              "input": "$lastViewed",
                              "as": "lastViewed",
                              "cond": { $and: [{ "$eq": ["$$lastViewed.userId", mongoose.Types.ObjectId(userId)] }, { "$eq": ["$$lastViewed.groupId", '$_id'] }] }
                          }
                      }
                  }
  </code></pre>
  
  <p>I got the exact output as below for userId 58c796d4344b9da4dfbe027b </p>
  
  <pre><code>{
    "_id" : "5955ea4fd8099718330ab191"
  
      viewedDetails: [
          {
             "_id" : ObjectId("595218a7d346d27fb0bc1705"),
             "userId" : ObjectId("58c796d4344b9da4dfbe027b"),
             "groupId" : ObjectId("5955ea4fd8099718330ab191"),
             "lastViewedTime" : ISODate("2017-06-19T09:39:07.374Z")
          }
        ]
  }
  </code></pre>
  
  <p>Now i changed my schema into and got $lookup result as</p>
  
  <pre><code>{
    "_id" : "5955ea4fd8099718330ab191"
  
      lastViewed: [
          {
             "_id" : ObjectId("595218a7d346d27fb0bc1705"),
             "userId" : ObjectId("58c796d4344b9da4dfbe027b"),
             "members":[
                    {
                      "groupId" : ObjectId("5955ea4fd8099718330ab162"),
                      "lastViewedTime" : ISODate("2017-05-11T09:39:07.374Z")
                    }
                    {
                      "groupId" : ObjectId("5955ea4fd8099718330ab191"),
                      "lastViewedTime" : ISODate("2016-05-19T09:39:07.374Z")
                    }
                ]               
          }
        ]
  }
  </code></pre>
  
  <p>then my filter for array of objects is not working , my query is</p>
  
  <pre><code> $project: { "viewedDetails": {
                          "$filter": {
                              "input": "$lastViewed",
                              "as": "lastViewed",
                              "cond": { $and: [{ "$eq": ["$$lastViewed.userId", mongoose.Types.ObjectId(userId)] }, 
                                               { "$eq": ["$$lastViewed.members.groupId", '$_id'] }] }
                          }
                      }
                  },
  </code></pre>
  
  <p>How can i get this without using $unwind, then using $project.</p>