Example usage for com.mongodb.async.client MongoCollection insertMany

List of usage examples for com.mongodb.async.client MongoCollection insertMany

Introduction

In this page you can find the example usage for com.mongodb.async.client MongoCollection insertMany.

Prototype

void insertMany(List<? extends TDocument> documents, SingleResultCallback<Void> callback);

Source Link

Document

Inserts one or more documents.

Usage

From source file:com.example.App.java

License:Apache License

private static Mono<List<Document>> create100Users(MongoCollection<Document> collection, Mono<?> mono) {
    return mono.then(Mono.create(sink -> {
        final List<Document> documents = IntStream.range(1, 100)
                .mapToObj(index -> String.format("user-%d", index))
                .map(name -> new Document("id", UUID.randomUUID()).append("name", name).append("created",
                        LocalDateTime.now()))
                .collect(Collectors.toList());
        final int size = documents.size();
        logger.info("documents to be saved: {}..{}", documents.get(0), documents.get(size - 1));
        collection.insertMany(documents, (v, t) -> {
            if (t == null) {
                logger.info("inserted: {}..{}", documents.get(0), documents.get(size - 1));
                sink.success(documents);
            } else {
                logger.error("error", t);
                sink.error(t);//from  w  w w  .j a  v  a2  s .c o  m
            }
        });
    }));
}