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

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

Introduction

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

Prototype

Boolean bypassDocumentValidation

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

Click Source Link

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insert(Value request) throws FaultException {
    try {/*from   ww  w  . ja va2s  .co  m*/
        Value v = Value.create();
        String collectionName = request.getFirstChild("collection").strValue();
        BsonDocument bsonDocument = createDocument(request.getFirstChild("document"));
        printlnJson("insert document", 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")) {
            InsertOneOptions insertOneOptions = new InsertOneOptions();
            insertOneOptions.bypassDocumentValidation(
                    request.getFirstChild("options").getFirstChild("bypassDocumentValidation").boolValue());
            db.getCollection(collectionName, BsonDocument.class).insertOne(bsonDocument, insertOneOptions);
        } else {
            db.getCollection(collectionName, BsonDocument.class).insertOne(bsonDocument);

        }

        bsonDocument.get("_id").asObjectId().getValue().toByteArray();

        String str = new String(bsonDocument.get("_id").asObjectId().getValue().toHexString());
        Value objValue = Value.create(str);
        v.getNewChild("_id").assignValue(objValue);
        v.getFirstChild("_id").getNewChild("@type").assignValue(Value.create("ObjectId"));

        return v;
    } catch (MongoException ex) {
        throw new FaultException("MongoException", ex);
    }

}