Skip to content

@MongoId not working for aggregate queries #4043

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
nniesen opened this issue May 5, 2022 · 1 comment
Closed

@MongoId not working for aggregate queries #4043

nniesen opened this issue May 5, 2022 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@nniesen
Copy link
Contributor

nniesen commented May 5, 2022

The @MongoId annotation is not being taken into account for aggregate queries like it is for find queries. This causes incorrect results for the aggregation match query because it's improperly looking for an ObjectId instead of a plain String; i.e., it's improperly wrapping the value in an { "$oid" : "<ObjectId bytes>"}.

Using:

+--- org.springframework.boot:spring-boot-dependencies:2.6.7
|    +--- org.springframework.boot:spring-boot-starter-data-mongodb:2.6.7 (c)
|    +--- org.springframework.boot:spring-boot-starter:2.6.7 (c)
|    +--- org.mongodb:mongodb-driver-sync:4.4.2 (c)
|    +--- org.springframework.data:spring-data-mongodb:3.3.4 (c)

Example code:

@SpringBootApplication
@EnableMongoRepositories
public class DemoApplication implements CommandLineRunner {
    @Autowired
    MongoTemplate mongoTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Criteria criteria = Criteria.where("users").elemMatch(Criteria.where("id").is("4ee921aca44fd11b3254e001"));

        mongoTemplate.aggregate(Aggregation.newAggregation(Aggregation.match(criteria)), Widget.class, Widget.class);
        mongoTemplate.find(new Query(criteria), Widget.class);
    }
}

@Document
class Widget {
    @Id
    String id;
    List<UserRef> users;
}

class UserRef {
    @MongoId
    String id;
    String name;
}

Generated queries (logging.level.org.springframework.data.mongodb.core: DEBUG)
Actual: { "_id" : { "$oid" : "4ee921aca44fd11b3254e001"}}

Executing aggregation: [{ "$match" : { "users" : { "$elemMatch" : { "_id" : { "$oid" : "4ee921aca44fd11b3254e001"}}}}}] in collection widget
find using query:                    { "users" : { "$elemMatch" : { "_id" : "4ee921aca44fd11b3254e001"}}} fields: Document{{}} for class: class demo.Widget in collection: widget

Expected: { "_id" : "4ee921aca44fd11b3254e001"}:

Executing aggregation: [{ "$match" : { "users" : { "$elemMatch" : { "_id" : "4ee921aca44fd11b3254e001"}}}}] in collection widget
find using query:                    { "users" : { "$elemMatch" : { "_id" : "4ee921aca44fd11b3254e001"}}} fields: Document{{}} for class: class demo.Widget in collection: widget
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 5, 2022
@nniesen nniesen changed the title @MongoId not applied for aggregate queries @MongoId not working for aggregate queries May 9, 2022
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 9, 2022
@christophstrobl christophstrobl self-assigned this May 9, 2022
@christophstrobl
Copy link
Member

Thanks for reporting and long silence. Good catch - this seems to have happened by a 2nd mapping pass done in MongoTemplate which then converted the _id from String to ObjectId.
A snapshot build for 4.1.0-GH-4043-SNAPSHOT should be available soon if you want to give it a try.

mp911de pushed a commit that referenced this issue Jan 11, 2023
A 2nd pass is no longer needed as the context already does all the work.

Closes: #4043
Original pull request: #4240
mp911de pushed a commit that referenced this issue Jan 11, 2023
A 2nd pass is no longer needed as the context already does all the work.

Closes: #4043
Original pull request: #4240
@mp911de mp911de added this to the 3.4.7 (2021.2.7) milestone Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants