Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-37246

$arrayElemAt aggregation, referencing property within an array element object, indexes don't include elements without that property.

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.8
    • Component/s: Aggregation Framework
    • None
    • ALL
    • Hide

      db.test.insert(
         {
            "_id" : ObjectId("5ba4d6ca79f29bad7b3fa913"),
            "firstArray": [
               {
               },
               {
                  "secondArray": ["a6", "a7", "a8", "a9", "a10"]
               },
               {
                  "secondArray": ["a11", "a12", "a13", "a14", "a15"]
               },
            ]
         }
      )

       

      Then run the following aggregation:

      db.test.aggregate([
         {
            "$match": {
               "_id": ObjectId("5ba4d6ca79f29bad7b3fa913")
      {{      }}}
         },
         {
            "$project": {
               "array": {
                  "$arrayElemAt": ["$firstArray.secondArray", 0]
               }
      {{      }}}
         }
      ])

      Returns:

      {{{}}
         "array": ["a6", "a7", "a8", "a9", "a10"]
      }

      This is confusing. I would expect it to return:

      {{{}}
         "array": null
      }

      or whatever mongo uses to indicate the aggregate returns nothing.

       

      The reason I expect it to return nothing, is because the first element of firstArray }}doesn't contain a property {{secondArray, so the index of 0 should first be selecting the first element of firstArray (i.e., ,) and then return secondArray which doesn't exit.

       

      What is actually happening is mongo is getting all the array elements that contain secondArray and then applying the index to that array. Hence why }}{{"$arrayElemAt": ["$firstArray.secondArray", 2] returns nothing, and not the last element in firstArray.

       

      Am I missing something? The documentation doesn't make it clear that this is the expected behaviour. Either the documentation should be updated to make it clear what the index supplied to $arrayElemAt actually applies to or this bug should be fixed.

      Many thanks!

       

       

       

       

       

      Show
      db.test.insert(    {       "_id" : ObjectId("5ba4d6ca79f29bad7b3fa913"),       "firstArray": [          {          },          {             "secondArray": ["a6", "a7", "a8", "a9", "a10"]          },          {             "secondArray": ["a11", "a12", "a13", "a14", "a15"]          },       ]     } )   Then run the following aggregation: db.test.aggregate([    {       "$match": {          "_id": ObjectId("5ba4d6ca79f29bad7b3fa913") {{      }}}    },    {       "$project": {          "array": {             "$arrayElemAt": ["$firstArray.secondArray", 0]           } {{      }}}     } ]) Returns: {{{}}    "array":  ["a6", "a7", "a8", "a9", "a10"] } This is confusing. I would expect it to return: {{{}}    "array": null } or whatever mongo uses to indicate the aggregate returns nothing.   The reason I expect it to return nothing, is because the first element of firstArray }}doesn't contain a property {{secondArray , so the index of 0 should first be selecting the first element of firstArray (i.e.,  , ) and then return  secondArray which doesn't exit.   What is actually happening is mongo is getting all the array elements that contain  secondArray  and then applying the index to that array. Hence why  }}{{"$arrayElemAt": ["$firstArray.secondArray", 2]  returns nothing, and not the last element in firstArray.   Am I missing something? The documentation doesn't make it clear that this is the expected behaviour. Either the documentation should be updated to make it clear what the index supplied to  $arrayElemAt actually applies to or this bug should be fixed. Many thanks!          

      When using the $arrayElemAt to select an array element of an array and to then include an property within that array element object, I've noticed that the index only applies to elements of the array that include the property. It's easier if I explain this with an example so please see the Steps to reproduce.

            Assignee:
            nick.brewer Nick Brewer
            Reporter:
            sjom106 Sam Miller
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: