spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

MongoItemReader.setSort() doesn't works without json query string

Open MinJunKweon opened this issue 3 years ago • 0 comments

Bug description When using MongoItemReader with setQuery(Query), setSort(Map<String, Sort.Direction>), Sort doesn't works. If you wanna use setSort() method, you should use setQuery(String) method. (If you use MongoItemReaderBuilder, it might be jsonQuery())

Environment Spring Batch 4.3.5

Steps to reproduce

@Bean
public MongoItemReader<Item> sampleItemReader(MongoTemplate mongoTemplate) {
    HashMap<String, Sort.Direction> sorts = new HashMap<>();
    sorts.put("_id", Sort.Direction.ASC);

    return new MongoItemReaderBuilder<Item>()
                  .saveState(false)
                  .template(mongoTemplate)
                  .collection("items")
                  .targetType(Item.class)
                  .pageSize(50)
                  .query(Query.query(Criteria.where("author").is("minjunkweon")))
                  .sorts(sorts); // Note: It will doesn't works.
                  .build();
}

Expected behavior I think it should work too. But If Spring Data Query has sorts, I think sorts() method should be ignored. (use Query's sort definition) If you never mind it, let me create PR about this?

Minimal Complete Reproducible example Please see Steps to reproduce

MinJunKweon avatar Apr 15 '22 07:04 MinJunKweon