2

Consider a collection called 'scheme' with following documents. How to query all schemes with atleast element in prepaid array that contains "a" ? Here the first collection satisfies the criteria, since dataArray[0] has 'a'. The second collection does not. Thus, I want to query to find only such collections whole dataArray contains 'a' in one of its elements.

{
    "plan":{
        "dataArray" : [
                 {
                     "a" : "x",
                     "b" : "y",
                     "c" : "z"
                 },
                 {
                     "b" : "x",
                     "c" : "z"
                 }
            ],
        }
}
{
    "plan":{
        "dataArray" : [
            {
                "b" : "y",
                "c" : "z"
            },
            {
                "b" : "x",
                "c" : "z"
            }
        ],
    }
}
Ashh
  • 44,693
  • 14
  • 105
  • 132
Tanvi Jaywant
  • 375
  • 1
  • 6
  • 18

1 Answers1

4

You can use $exists operator here to check whether the property exists in the array or not

db.collection.find({
  "plan.dataArray.a": {
    $exists: true
  }
})

Output

[
  {
    "plan": {
      "dataArray": [
        {
          "a": "x",
          "b": "y",
          "c": "z"
        },
        {
          "b": "x",
          "c": "z"
        }
      ]
    }
  }
]
Ashh
  • 44,693
  • 14
  • 105
  • 132