Summary
Issue is reproduced when using the 5.4.0 driver.
When creating a mongo path to a property containing a filtered positional operator, the resulting path may be incorrect. The following snippet shows how to reproduce an incorrect path:
import com.mongodb.kotlin.client.model.filteredPosOp import com.mongodb.kotlin.client.model.path import org.bson.types.ObjectId data class Document( val _id: ObjectId, val elements: List<String>, ) fun main() { var incorrectPathFound = false var index = 0 while (!incorrectPathFound) { val path = Document::elements.filteredPosOp("identifier$index").path() val expectedPath = "elements.$[identifier$index]" incorrectPathFound = path != expectedPath if (incorrectPathFound) { println("Incorrect path for index $index: path is $path but should be $expectedPath") } index++ } }
The snippet can be found this repo: https://github.com/brigondaud-betclic/mongo-java-driver-5.4.0-bug
A possible output of this snippet is:
Incorrect path for index 102115: path is elements.$[identifier58628] but should be elements.$[identifier102115]
Issue seems related to instances of KPropertyPath.CustomProperty. When resolving a path using the com.mongodb.kotlin.client.model.path method, instances of this class are stored in the pathCache using the default toString method. A cache hit may happen in case of hashCode conflicts when resolving another path with a CustomProperty instance, producing an incorrect property name.