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

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

Introduction

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

Prototype

InsertOneOptions

Source Link

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insert(Value request) throws FaultException {
    try {//from   w  w  w  .j av  a 2 s.  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);
    }

}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public boolean logAction(LogEntry entry) {
    return call(() -> {
        MongoCollection<Document> c = database.getCollection("action");

        Document doc = new Document().append("timestamp", entry.getTimestamp())
                .append("actor", entry.getActor()).append("actorName", entry.getActorName())
                .append("type", Character.toString(entry.getType())).append("actedName", entry.getActedName())
                .append("action", entry.getAction());

        if (entry.getActed() != null) {
            doc.append("acted", entry.getActed());
        }// w ww. j  a v a2  s.  com

        c.insertOne(doc, new InsertOneOptions());
        return true;
    }, false);
}

From source file:org.apache.eagle.alert.engine.publisher.dedup.MongoDedupEventsStore.java

License:Apache License

@Override
public void add(EventUniq eventEniq, ConcurrentLinkedDeque<DedupValue> dedupStateValues) {
    try {/*from   www .jav a  2s .  c om*/
        BsonDocument doc = TransformerUtils
                .transform(new DedupEntity(this.publishName, eventEniq, dedupStateValues));
        BsonDocument filter = new BsonDocument();
        filter.append(DEDUP_ID, new BsonInt64(TransformerUtils.getUniqueId(this.publishName, eventEniq)));
        Document returnedDoc = stateCollection.findOneAndReplace(filter, Document.parse(doc.toJson()));
        if (returnedDoc == null) {
            InsertOneOptions option = new InsertOneOptions();
            stateCollection.insertOne(Document.parse(doc.toJson()), option);
        }
    } catch (Exception e) {
        LOG.error("insert dedup state failed, but the state is still in memory, could be ingored.", e);
    }
}