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

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

Introduction

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

Prototype

boolean ordered

To view the source code for com.mongodb.client.model InsertManyOptions ordered.

Click Source Link

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insertMany(Value request) throws FaultException {
    Value v = Value.create();/*from   ww w. j av  a  2s  . c o m*/
    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:org.apache.storm.mongodb.common.MongoDBClient.java

License:Apache License

/**
 * Inserts one or more documents.//w  ww .  j  a  v a2s  .  co m
 * 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);
}