Example usage for org.springframework.data.mongodb.core ReactiveMongoTemplate insert

List of usage examples for org.springframework.data.mongodb.core ReactiveMongoTemplate insert

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core ReactiveMongoTemplate insert.

Prototype

public <T> Flux<T> insert(Collection<? extends T> batchToSave, String collectionName) 

Source Link

Usage

From source file:com.github.danielfernandez.matchday.data.Data.java

public static void initializeAllData(final ReactiveMongoTemplate mongoTemplate) {

    /*/*from   ww w  .j a  v  a 2s  . c  om*/
     *  Drop collections, then create them again
     */
    final Mono<Void> initializeCollections = mongoTemplate.dropCollection(Team.class)
            .then(mongoTemplate.dropCollection(Match.class)).then(mongoTemplate.dropCollection(Player.class))
            .then(mongoTemplate.dropCollection(MatchEvent.class))
            .then(mongoTemplate.dropCollection(MatchComment.class))
            .then(mongoTemplate.createCollection(Team.class)).then(mongoTemplate.createCollection(Match.class))
            .then(mongoTemplate.createCollection(Player.class))
            .then(mongoTemplate.createCollection(MatchEvent.class,
                    CollectionOptions.empty().size(104857600).capped())) // max: 100MBytes
            .then(mongoTemplate.createCollection(MatchComment.class,
                    CollectionOptions.empty().size(104857600).capped())) // max: 100MBytes
            .then();

    /*
     * Add some test data to the collections: teams and players will come from the
     * utility Data class, but we will generate matches between teams randomly each
     * time the application starts (for the fun of it)
     */
    final Mono<Void> initializeData = mongoTemplate
            // Insert all the teams into the corresponding collection and log
            .insert(Data.TEAMS, Team.class).log(LOGGER_INITIALIZE, Level.FINEST)
            // Collect all inserted team codes and randomly shuffle the list
            .map(Team::getCode).collectList().doOnNext(Collections::shuffle)
            .flatMapMany(list -> Flux.fromIterable(list))
            // Create groups of two teams and insert a new Match for them
            .buffer(2).map(twoTeams -> new Match(twoTeams.get(0), twoTeams.get(1)))
            .flatMap(mongoTemplate::insert).log(LOGGER_INITIALIZE, Level.FINEST)
            .concatMap(match -> mongoTemplate
                    .insert(new MatchEvent(match.getId(), MatchEvent.Type.MATCH_START, null, null)))
            // Finally insert the players into their corresponding collection
            .thenMany(Flux.fromIterable(Data.PLAYERS)).flatMap(mongoTemplate::insert)
            .log(LOGGER_INITIALIZE, Level.FINEST).then();

    /*
     * Perform the initialization, blocking (that's OK, we are bootstrapping a testing app)
     */
    initializeCollections.then(initializeData).block();

}