spring-projects / spring-projects/spring-batch

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

Open
#4,097 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: infrastructure related-to: item-readers-writers status: need-help-to-reproduce status: waiting-for-reporter type: bug
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MongoItemReader and MongoItemReaderBuilder entry points described in the reproduction, comparing setQuery(Query) with setQuery(String) or jsonQuery(). Reproduce the sorting case using Query and sorts, then verify that sorting works and that any sort already defined on Query takes precedence or the documented behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb, spring
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.