Search code examples
javamongodbspring-batchspring-data-mongodb

How to read all documents of mongo collection using Spring Batch?


I have to read an entire collection from MongoDB without filters during spring batch.

I have created a class XyzMongoItemReader that extends MongoItemReader on XyzMongoItemReader constructor I'm setting the following field:

public XyzMongoItemReader(
        final MongoTemplate template,
        final Query query) {
    setTemplate(template);
    setQuery(query);
    setTargetType(Xyz.class);
}

The Xyz class is annotated with @Document and fields are annotated with @Field:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Xyz {

    @Field
    @JsonProperty
    protected String x;
    
    @Field
    @JsonProperty
    private String y;

    @Field
    @JsonProperty
    private Z z;

}

The configuration class is:

@Configuration
@EnableBatchProcessing
public class XyzBatchConfigurerReaderFromMongoWriterToRedis extends AbstractBatchConfigurer<Xyz, String> {

    @Autowired
    MongoTemplate mongoTemplate;

    @StepScope
    @Bean
    @Override
    public ItemReader<Xyz> reader() throws Exception {
        return new XyzMongoItemReader(mongoTemplate, new Query());
    }

    ... processor ...

    ... writer ...

}

The collection on Mongo has 20 records, but when the read method is executed, there is no document retrieved.

Is it sufficient to set those 3 parameters?

  • template
  • query
  • targetType

Is "new Query()" the right query to use to be able to read all the document on the collection?


Solution

  • I didn't set the Collection...

    public XyzMongoItemReader(
            final MongoTemplate template,
            final Query query) {
        setTemplate(template);
        setCollection(Xyz.class.getSimpleName());
        setQuery(query);
        setTargetType(Xyz.class);
    }
    

    Reader:

    @StepScope
    @Bean
    @Override
    public ItemReader<Xyz> reader() throws Exception {
        return new XyzMongoItemReader(mongoTemplate, new Query());
    }
    

    using queryString instead of Query requires also to setSort:

    public XyzMongoItemReader(
            final MongoTemplate template,
            final String query, 
            final Map<String, Sort.Direction> sorts) {
        setTemplate(template);
        setCollection(Xyz.class.getSimpleName());
        setQuery(query);
        setSort(sorts);
        setTargetType(parametrizedType);
    }
    

    Reader:

    @StepScope
    @Bean
    @Override
    public ItemReader<Xyz> reader() throws Exception {
        return new XyzMongoItemReader(mongoTemplate, "{}", Map.of("_id", Sort.Direction.ASC));
    }