[JAVA-5100] Id property mapping is not applied to index selection Created: 08/Aug/23  Updated: 20/Nov/23  Resolved: 14/Aug/23

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 4.9.1
Fix Version/s: None

Type: Question Priority: Unknown
Reporter: Nathan Niesen Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 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.



 Comments   
Comment by Nathan Niesen [ 20/Nov/23 ]

For posterity: https://github.com/spring-projects/spring-data-mongodb/issues/4557

Comment by Jeffrey Yemin [ 14/Aug/23 ]

nathan.niesen@datasite.com, the annotations and query builder classes that you reference in the description look like they are provided by Spring Data MongoDB, not the Java driver itself.  I suggest following up at https://github.com/spring-projects/spring-data-mongodb/issues. 

Comment by Nathan Niesen [ 09/Aug/23 ]

TL;DR; The driver lets us write queries using entity model naming (id) but it doesn't do the same for choosing the index so we must still write the queries using the db naming (_id) if we want the query to use an index.

Comment by PM Bot [ 08/Aug/23 ]

Hi nathan.niesen@datasite.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:03:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.