Example usage for com.mongodb.client.model InsertManyOptions InsertManyOptions

List of usage examples for com.mongodb.client.model InsertManyOptions InsertManyOptions

Introduction

In this page you can find the example usage for com.mongodb.client.model InsertManyOptions InsertManyOptions.

Prototype

InsertManyOptions

Source Link

Usage

From source file:com.eightkdata.mongowp.client.wrapper.MongoConnectionWrapper.java

License:Open Source License

@Override
public void asyncInsert(String database, String collection, boolean continueOnError,
        List<? extends BsonDocument> docsToInsert) throws MongoException {
    try {//from   www .jav  a 2s  . c om
        owner.getDriverClient().getDatabase(database).getCollection(collection, BsonDocument.class)
                .insertMany(docsToInsert, new InsertManyOptions().ordered(continueOnError));
    } catch (com.mongodb.MongoException ex) { //a general Mongo driver exception
        if (ErrorCode.isErrorCode(ex.getCode())) {
            throw toMongoException(ex);
        } else {
            throw toRuntimeMongoException(ex);
        }
    }
}

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));
        }// w w  w  .j  av a2 s.c o  m
        batch.add(doc);
    }

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

From source file:com.mastfrog.asyncpromises.mongo.CollectionPromises.java

License:Open Source License

/**
 * Create a promise to insert many elements when passed the elements to
 * insert.//from  ww w  .  j  ava2  s. c  o  m
 *
 * @return A promise
 */
public AsyncPromise<List<T>, Void> insertMany() {
    return insertMany(new InsertManyOptions());
}

From source file:io.lumeer.storage.mongodb.MongoDbStorage.java

License:Open Source License

@Override
public List<String> createDocuments(final String collectionName, final List<DataDocument> dataDocuments) {
    List<Document> documents = dataDocuments.stream().map(MongoUtils::dataDocumentToDocument)
            .collect(Collectors.toList());

    database.getCollection(collectionName).insertMany(documents, new InsertManyOptions().ordered(false));

    return documents.stream().filter(d -> d.containsKey(DOCUMENT_ID))
            .map(d -> d.getObjectId(DOCUMENT_ID).toString()).collect(Collectors.toList());
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insertMany(Value request) throws FaultException {
    Value v = Value.create();/*  ww w .j av  a  2s .  c om*/
    String collectionName = request.getFirstChild("collection").strValue();
    List<BsonDocument> documents = new ArrayList();
    BsonDocument bsonDocument;
    try {

        for (int counterDocuments = 0; counterDocuments < request.getChildren("document")
                .size(); counterDocuments++) {
            bsonDocument = createDocument(request.getChildren("document").get(counterDocuments));
            documents.add(bsonDocument);
        }
        if (request.hasChildren("writeConcern")) {
            WriteConcern writeConcern = new WriteConcern();
            if (request.getFirstChild("writeConcern").hasChildren("journal")) {
                writeConcern.withJournal(
                        request.getFirstChild("writeConcern").getFirstChild("journal").boolValue());
            }
            if (request.getFirstChild("writeConcern").hasChildren("w")) {
                if (request.getFirstChild("writeConcern").getFirstChild("w").isInt()) {
                    writeConcern.withW(request.getFirstChild("writeConcern").getFirstChild("w").intValue());
                }
                if (request.getFirstChild("writeConcern").getFirstChild("w").isString()) {
                    writeConcern.withW(request.getFirstChild("writeConcern").getFirstChild("w").strValue());
                }
            }
            if (request.getFirstChild("writeConcern").hasChildren("timeout")) {
                writeConcern.withWTimeout(
                        request.getFirstChild("writeConcern").getFirstChild("timeout").longValue(),
                        TimeUnit.MILLISECONDS);
            }

            db.getCollection(collectionName, BsonDocument.class).withWriteConcern(writeConcern);
        }
        if (request.hasChildren("options")) {
            InsertManyOptions insertManyOptions = new InsertManyOptions();
            insertManyOptions.ordered(request.getFirstChild("options").getFirstChild("ordered").boolValue());
            insertManyOptions.ordered(request.getFirstChild("options").getFirstChild("ordered").boolValue());
            insertManyOptions.bypassDocumentValidation(
                    request.getFirstChild("options").getFirstChild("bypassDocumentValidation").boolValue());
            db.getCollection(collectionName, BsonDocument.class).insertMany(documents, insertManyOptions);
        } else {
            db.getCollection(collectionName, BsonDocument.class).insertMany(documents);

        }
        ;
        db.getCollection(collectionName, BsonDocument.class).insertMany(documents);
        for (int counterDocuments = 0; counterDocuments < request.getChildren("document")
                .size(); counterDocuments++) {
            String str = new String(Hex.decodeHex(documents.get(counterDocuments).get("_id").asObjectId()
                    .getValue().toHexString().toCharArray()), StandardCharsets.UTF_8);
            Value result = Value.create();
            result.getNewChild("_id").assignValue(Value.create(str));
            result.getFirstChild("_id").getNewChild("@type").assignValue(Value.create("ObjectID"));
            v.getChildren("results").add(result);
        }

    } catch (MongoException ex) {
        throw new FaultException("MongoException", ex);
    } catch (DecoderException ex) {
        Logger.getLogger(MongoDbConnector.class.getName()).log(Level.SEVERE, null, ex);
    }
    return v;
}

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;//from  w w w  .  j  a v  a2s.com
    }

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

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

From source file:org.apache.rya.mongodb.batch.collection.MongoCollectionType.java

License:Apache License

@Override
public void insertMany(final List<Document> items) {
    collection.insertMany(items, new InsertManyOptions().ordered(false));
}

From source file:org.apache.storm.mongodb.common.MongoDBClient.java

License:Apache License

/**
 * Inserts one or more documents.// w  w w  . ja v  a 2s  .com
 * This method is equivalent to a call to the bulkWrite method.
 * The documents will be inserted in the order provided, 
 * stopping on the first failed insertion. 
 * 
 * @param documents
 */
public void insert(List<Document> documents, boolean ordered) {
    InsertManyOptions options = new InsertManyOptions();
    if (!ordered) {
        options.ordered(false);
    }
    collection.insertMany(documents, options);
}

From source file:org.axonframework.mongo.eventsourcing.tokenstore.MongoTokenStore.java

License:Apache License

@Override
public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken)
        throws UnableToClaimTokenException {
    if (fetchSegments(processorName).length > 0) {
        throw new UnableToClaimTokenException(
                "Unable to initialize segments. Some tokens were already present for the given processor.");
    }// w w w  . j  a va  2 s  . c  o  m

    List<Document> entries = IntStream.range(0, segmentCount).mapToObj(
            segment -> new GenericTokenEntry<>(initialToken, serializer, contentType, processorName, segment))
            .map(this::tokenEntryToDocument).collect(Collectors.toList());
    mongoTemplate.trackingTokensCollection().insertMany(entries, new InsertManyOptions().ordered(false));

}