[SERVER-62242] $indexOfArray does not work with duplicate values in array Created: 23/Dec/21  Updated: 29/Oct/23  Resolved: 07/Jan/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 4.4.14, 5.3.0, 5.0.7, 4.2.20, 5.2.2

Type: Bug Priority: Major - P3
Reporter: Jan Votava Assignee: Steve Tarzia
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v5.2, v5.0, v4.4, v4.2, v4.0
Steps To Reproduce:

db.test.insertOne({
  number: 234
})
 
db.test.aggregate([
  { $addFields: {
    test: {
      $indexOfArray: [[123, 123, 234], "$number"]
    }
  } },
])

Sprint: QO 2022-01-10
Participants:

 Description   

When array with duplicit values is supplied as a first argument to $indexOfArray, it starts returning `-1` when searching for subsequent values.



 Comments   
Comment by Githook User [ 09/Mar/22 ]

Author:

{'name': 'Steve Tarzia', 'email': 'steve.tarzia@mongodb.com', 'username': 'starzia'}

Message: SERVER-62242 fixed $indexOfArray handling of duplicate vals in const arrays
Branch: v4.2
https://github.com/mongodb/mongo/commit/3fe0851620a782f703e722e37afa05460986c81f

Comment by Githook User [ 09/Mar/22 ]

Author:

{'name': 'Steve Tarzia', 'email': 'steve.tarzia@mongodb.com', 'username': 'starzia'}

Message: SERVER-62242 fixed $indexOfArray handling of duplicate vals in const arrays
Branch: v4.4
https://github.com/mongodb/mongo/commit/c37766684398134cd3a671f9de498e12ff98a500

Comment by Githook User [ 09/Mar/22 ]

Author:

{'name': 'Steve Tarzia', 'email': 'steve.tarzia@mongodb.com', 'username': 'starzia'}

Message: SERVER-62242 fixed $indexOfArray handling of duplicate vals in const arrays
Branch: v5.0
https://github.com/mongodb/mongo/commit/c19670be86ea5a2a59f421a1b8293028ee5c392d

Comment by Githook User [ 09/Mar/22 ]

Author:

{'name': 'Steve Tarzia', 'email': 'steve.tarzia@mongodb.com', 'username': 'starzia'}

Message: SERVER-62242 fixed $indexOfArray handling of duplicate vals in const arrays
Branch: v5.2
https://github.com/mongodb/mongo/commit/fe13637b2bf0ed26970d1103c6e9254c0887c48a

Comment by Githook User [ 07/Jan/22 ]

Author:

{'name': 'Steve Tarzia', 'email': 'steve.tarzia@mongodb.com'}

Message: SERVER-62242 fixed $indexOfArray handling of duplicate vals in const arrays
Branch: master
https://github.com/mongodb/mongo/commit/f6e0a99e265b4ec58c2fb8a902d74b3151d1b6f0

Comment by Edwin Zhou [ 31/Dec/21 ]

Hi cyner@overflow.cz,

Thank you for your report. I'm able to reproduce this issue on v5.0.5. This appears to happen only when there are duplicate values in the array and the specified search expression uses a field path. I will assign this ticket to the appropriate team.

Best,
Edwin

Comment by Jan Votava [ 23/Dec/21 ]

Version 5.0.4, results in

[
  {
    "_id": {
      "$oid": "61c42f76aa4309f542ee794c"
    },
    "number": 234,
    "test": -1
  }
]

Generated at Thu Feb 08 05:54:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.