Devidb
Devidb

Reputation: 112

Use $addToSet with Spring Batch MongoItemWriter

I'm currently implementing a Spring Batch application with a MongoItemWriter, and I need to add some logic in the mongo upsert query, like adding a field in an array only if it does not exist ($addToSet if I'm not mistaken).

My first idea was to override the MongoItemWriter to change the saveOrUpdate method behavior while keeping the benefits of the existing spring batch bulk configuration. But all this part is private and so I have to rewrite all if I want to change the mongo query.

Another solution could be to implement the addToSet logic in the processor with a find, but it seems not optimized (one find query by item).

It is very strange that Spring Batch does not allow to configure easily the mongo query (that offers a lot of possibility) while keeping the existing transaction and bulk configuration.

Is there something I am missing here?

Upvotes: 0

Views: 252

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31600

The doWrite and getTemplate methods are protected, so you can override doWrite and use the template as needed (without having to deal with item buffering or transaction management which are handled in the write method).

With that, you should have full control over item writing and you can create/customize the query as needed. That said, if you think other methods could be opened for extension, please open a feature request and we can discuss how to improve the extensibility of the writer.

Upvotes: 1

Related Questions