I have the following ItemRader
and by passing collectionName and filter parameters from the step, I read 2 collections from mongodb.
However, I need to filter second collection based on the id value of the first filtered collection like retrieving data as cascading dropdown for country-city. When filtering 2 countries, will retrieve cities belonging to these countries.
I am new in Spring Batch and thought to update the filter and pass it to the next chain so that I can use the same reader for all 2 collections, but not sure if there is a better approach e.g. reading both of these collections in the same step and then write in the next step.
@Component
@StepScope
public class LocationItemReader extends MongoCursorItemReader<Document> {
private StepExecution stepExecution;
private final String collectionName;
private final String filter;
public LocationItemReader(@Qualifier("mongoTemplate") MongoTemplate mongoTemplate,
@Value("#{collectionName}") String collectionName,
@Value("#{jobParameters['filter']}") String filter,
MongoQueryBuilder queryBuilder) {
this.collectionName = collectionName;
this.filter = filter;
// code omitted
// I thought to update filter based on the first query result if collectionName is equal to the second collection
setQuery(queryBuilder.buildQuery(filter));
}
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@AfterStep
public void afterStep() {
stepExecution.setExitStatus(ExitStatus.COMPLETED);
}
}
1