Using Spring you can easily store multiple documents at once.
The Interface is already available with method saveAll and details as under:
@NoRepositoryBean
public interface MongoRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
*/
@Override
<S extends T> List<S> saveAll(Iterable<S> entites);
//...
}
Spring usage example:
@Component
public class Processor {
@Autowired
public Processor(Repository repository) {
this.repository= repository;
}
public void save(Iterable<ProductEntity> entites) {
List<ProductEntity> saved = repository.saveAll(entites);
logger.info("Saved {} entities", saved.size());
}
}
your Repository interface:
//https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html
public interface Repository extends MongoRepository<ProductEntity, String> {
}
Call save method with 'List' of Product entities
insertAll()
– Scalpel