Details
-
Question
-
Resolution: Won't Do
-
Unknown
-
None
-
4.9.1
-
None
-
None
Description
Summary
Id property mapping is not applied to index selection resulting in the query performing a collection scan instead of using the index.
Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).
mongo-driver-sync-4.9.1
Atlas 5.0.19, Standalone 5.0.3
How to Reproduce
Java classes:
public class SomeEntity { @MongoId(FieldType.IMPLICIT) private Long id; // We use @Id String but that is irrelevant for the example |
private SomeRef someRef; |
}
|
public class SomeRef { |
@MongoId(FieldType.IMPLICIT) |
private String id; |
private String name; |
}
|
Some data:
{ "_id": 123, { "_id': "a", "name": "Apple" } }{ "_id": 456, { "_id': "b", "name": "Banana" } }
|
Add index:
db.someEntity.createIndex({ "someRef._id": 1}, {background: true});
|
creates an index named: someRef._id_1
|
Perform a query:
mongoTemplate.find(new Query(Criteria.where("someRef.id").is("a")), someEntity.class) |
The logged query is
{ "someRef.id": "a" }
|
it returns record 123 but it results in a collection scan because it can't find an index for "someRef.id". The driver mapped the "someRef.id" to "someRef._id" for the find but not for the index selection.
Additional Background
Please provide any additional background information that may be helpful in diagnosing the bug.