How to return all documents where an array field's size is greater than a given number?
<p>I have a document like </p>

<pre><code>{
    "_id": ObjectId('56a77bfae0ce9f6a738cb2b7'),
    "nameIdentity": [
        {
            "givenNameOne": "LATANYA",
            "givenNameThree": "BIZOR",
            "lastName": "BIZOR",
            "sourceReferenceId": [
                {
                    "sourceReferenceId": "56a77bfae0ce9f6a738cb2b5",
                    "sourceName": "A"
                },
                {
                    "sourceReferenceId": "56a77bfae0ce9f6a738cb2b5",
                    "sourceName": "B"
                }
            ]
        }
    ]
}
</code></pre>

<p>nameIdentity is an array and sourceReferenceId is an nested array inside nameIdentity. I am trying to get the documents whose sourceReferenceId size is greater than 2.
I used i aggregation like this :</p>

<pre><code>db.entity.aggregate(
   [
      {
         $project: {
            nameIdentity_count: {$size: "$nameIdentity.sourceReferenceId"}
         }
      },
      {
        "$match": {
            "nameIdentity_count": { "$gte": 2 }
         }
    }
   ]
)
</code></pre>

<p>this is not working as expected, it gives the document even with one soureReferenceId.Can anyone tell me where am i going wrong?</p>