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

$indexOfArray (aggregation) returns incorrect index

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • 4.4.19, 6.0.5
    • 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

    Description

      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. 

      Attachments

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

        Activity

          People

            backlog-query-execution Backlog - Query Execution
            kev_bite@msn.com Kevin Smith
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: