Skip to content

Projection on the _id field returns wrong result when using @MongoId (MongoDB 4.4) #3668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
steschae opened this issue Jun 14, 2021 · 1 comment
Assignees
Labels
type: regression A regression from a previous release

Comments

@steschae
Copy link

Describe the bug
Executing a MongoDB query against MongoDB 4.4 which contains a projection on the _id field with an entity class using the @MongoId annotation on a string-id field results in returned objects which have incorrect id values. In fact, the id values of all returned objects all have the same value "1".
However, when using the @Id instead of the @MongoId annotation, the same query returns the expected results containing the correct id values.

Expected behavior
The returned objects each have their correct id value.

Actual behavior
When using MongoDB 4.4: The returned objects each have the id value "1"
When using MongoDB 4.2: The returned objects each have their correct id value.

To Reproduce
I have set up an example project with test cases (https://github.com/steschae/spring-data-mongodb-id-projection-bug). The test "MongoDbVersion4_4_Test > should_find_all_person_ids_when_using_mongo_id_annotation" is the one which is failing because of this bug (https://github.com/steschae/spring-data-mongodb-id-projection-bug/runs/2818935410#step:4:33).

To reproduce, 1. create an entity class using the @MongoId annotation

@Data
public class Person {
    @MongoId
    private String id;
}
  1. Create a query with a projection on the _id field:
public interface PersonRepository extends CrudRepository<PersonWithMongoId, String> {
    @Query(value="{}", fields = "{ _id : 1 }")
    List<Person> findAllIds();
}

or

final var query = new Query();
query.fields().include(ID);
mongoTemplate.find(query, Person.class);
  1. When executing the query, all returned objects have the value "1" in the id field.

image

Configuration/Environment/Context
MongoDB 4.4
org.springframework.data:spring-data-mongodb:3.2.1
org.mongodb:mongodb-driver-sync:4.2.3

Additional information
With MongoDB 4.4 the behaviour of projection documents in MongoDB queries has changed:
(https://docs.mongodb.com/manual/release-notes/4.4-compatibility/#projection-compatibility-changes)

The query is translated to db.getCollection("persons").find({}, {"_id": "1"}) (the value of the _id projection is a string). This worked with MongoDB 4.2, but with MongoDB 4.4 the string value "1" in the projection causes the id value of the returned documents to be overidden with this value "1".
This happens somewhere in org.springframework.data.mongodb.core.convert.QueryMapper#getMappedValue, org.springframework.data.mongodb.core.convert.QueryMapper#convertId.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 14, 2021
@christophstrobl
Copy link
Member

thanks for reporting - we'll look into this.

@christophstrobl christophstrobl self-assigned this Jun 14, 2021
@mp911de mp911de added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 14, 2021
@mp911de mp911de added this to the 3.2.2 (2021.0.2) milestone Jun 21, 2021
mp911de pushed a commit that referenced this issue Jun 21, 2021
The field projection conversion should actually only map field names and avoid value conversion. In the MongoId case an inclusion parameter (1) was unintentionally converted into its String representation which causes trouble on Mongo 4.4 servers.

Fixes: #3668
Original pull request: #3678.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants