Skip to content

@MongoId not working for aggregate queries #4043

Closed
@nniesen

Description

@nniesen

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions