Example usage for com.mongodb.client MongoCollection insertMany

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

Introduction

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

Prototype

void insertMany(ClientSession clientSession, List<? extends TDocument> documents);

Source Link

Document

Inserts one or more documents.

Usage

From source file:com.facebook.presto.mongodb.MongoPageSink.java

License:Apache License

@Override
public CompletableFuture<?> appendPage(Page page) {
    MongoCollection<Document> collection = mongoSession.getCollection(schemaTableName);
    List<Document> batch = new ArrayList<>(page.getPositionCount());

    for (int position = 0; position < page.getPositionCount(); position++) {
        Document doc = new Document();

        for (int channel = 0; channel < page.getChannelCount(); channel++) {
            MongoColumnHandle column = columns.get(channel);
            doc.append(column.getName(),
                    getObjectValue(columns.get(channel).getType(), page.getBlock(channel), position));
        }//from www  . j ava2  s  . com
        batch.add(doc);
    }

    collection.insertMany(batch, new InsertManyOptions().ordered(true));
    return NOT_BLOCKED;
}

From source file:com.yahoo.ycsb.db3.MongoDbClient.java

License:Open Source License

/**
 * Insert a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record
 * key./*from  w  w w.  j a v a 2 s .c o  m*/
 * 
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to insert.
 * @param values
 *          A HashMap of field/value pairs to insert in the record
 * @return Zero on success, a non-zero error code on error. See the {@link DB}
 *         class's description for a discussion of error codes.
 */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document toInsert = new Document("_id", key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.put(entry.getKey(), entry.getValue().toArray());
        }

        if (batchSize == 1) {
            if (useUpsert) {
                // this is effectively an insert, but using an upsert instead due
                // to current inability of the framework to clean up after itself
                // between test runs.
                collection.replaceOne(new Document("_id", toInsert.get("_id")), toInsert, UPDATE_WITH_UPSERT);
            } else {
                collection.insertOne(toInsert);
            }
        } else {
            bulkInserts.add(toInsert);
            if (bulkInserts.size() == batchSize) {
                if (useUpsert) {
                    List<UpdateOneModel<Document>> updates = new ArrayList<UpdateOneModel<Document>>(
                            bulkInserts.size());
                    for (Document doc : bulkInserts) {
                        updates.add(new UpdateOneModel<Document>(new Document("_id", doc.get("_id")), doc,
                                UPDATE_WITH_UPSERT));
                    }
                    collection.bulkWrite(updates);
                } else {
                    collection.insertMany(bulkInserts, INSERT_UNORDERED);
                }
                bulkInserts.clear();
            } else {
                return OptionsSupport.BATCHED_OK;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        System.err.println("Exception while trying bulk insert with " + bulkInserts.size());
        e.printStackTrace();
        return Status.ERROR;
    }

}

From source file:net.netzgut.integral.mongo.utils.BatchOperations.java

License:Apache License

public static void replaceDocuments(MongoCollection<Document> collection, List<Document> documents) {

    // 1. Validation
    // BW decided emptying the collection via this method should always
    // be wrong, so ignore request if no new documents are coming.
    if (collection == null || documents == null || documents.isEmpty()) {
        return;// w ww. ja v  a2  s.c  om
    }

    // 2. Remove old data
    collection.deleteMany(new Document());

    // 3. Insert new data
    collection.insertMany(documents, new InsertManyOptions().ordered(true));
}