$indexOfArray (aggregation) returns incorrect index

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: 4.4.19, 6.0.5
    • Component/s: Aggregation Framework
    • None
    • Query Execution
    • ALL
    • Hide

      If we insert the following record to a collection

      ```javascript

      db.classes.insert({
          name: "Class A",
          students: [
           

      {         firstName: "Joe",         middleName: "Harry",         lastName: "Bar"       }

      ,
           

      {         firstName: "Mary",         lastName: "Molly"       }

      ,
           

      {         firstName: "Katie",         middleName: "Morgan",         lastName: "Smith"       }

          ]
        })

      ```

      Then run the following aggregation pipeline

      ```javascript

      db.collection.aggregate([
        {
          "$project": {
            studentIndex:

      {         "$indexOfArray": [           "$students.middleName",           "Morgan"         ]       }

          }
        }
      ])

      ```

      We'd expect that the following is returned

      ```javascript

      [
       

      {     "studentIndex": 2   }

      ]

      ```

      however the following is actually returned

      ```javascript

      [
       

      {     "studentIndex": 1   }

      ]

      ```

      As you can see the index is off by 1.

       

      https://mongoplayground.net/p/pCmXbXUkBOo

      Show
      If we insert the following record to a collection ```javascript db.classes.insert({     name: "Class A",     students: [       {         firstName: "Joe",         middleName: "Harry",         lastName: "Bar"       } ,       {         firstName: "Mary",         lastName: "Molly"       } ,       {         firstName: "Katie",         middleName: "Morgan",         lastName: "Smith"       }     ]   }) ``` Then run the following aggregation pipeline ```javascript db.collection.aggregate([   {     "$project": {       studentIndex: {         "$indexOfArray": [           "$students.middleName",           "Morgan"         ]       }     }   } ]) ``` We'd expect that the following is returned ```javascript [   {     "studentIndex": 2   } ] ``` however the following is actually returned ```javascript [   {     "studentIndex": 1   } ] ``` As you can see the index is off by 1.   https://mongoplayground.net/p/pCmXbXUkBOo
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      We've recently been writing a query to get back the index of an array item, however, it took us a while to realize that the index being returned was incorrect.

      It seems that if the expression that you give to `$indexOfArray` has none existent values then the array index will be off by 1.

      I'm not sure if this is by design but there's nothing in the documentation around this behavior. 

        1. image-2023-04-12-09-33-01-021.png
          31 kB
          Chris Kelly
        2. image-2023-04-12-09-38-47-007.png
          32 kB
          Chris Kelly
        3. image-2023-04-12-14-44-10-726.png
          41 kB
          Kevin Smith
        4. image-2023-04-12-14-45-58-724.png
          30 kB
          Kevin Smith

              Assignee:
              [DO NOT USE] Backlog - Query Execution
              Reporter:
              Kevin Smith
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: