Skip to content

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

Open
@MinJunKweon

Description

@MinJunKweon

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions