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

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

Introduction

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

Prototype

Boolean bypassDocumentValidation

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

Click Source Link

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insertMany(Value request) throws FaultException {
    Value v = Value.create();//from  w  w  w .  ja  v a  2s. co 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;
}