Example usage for com.mongodb WriteConcern withJournal

List of usage examples for com.mongodb WriteConcern withJournal

Introduction

In this page you can find the example usage for com.mongodb WriteConcern withJournal.

Prototype

public WriteConcern withJournal(final Boolean journal) 

Source Link

Document

Constructs a new WriteConcern from the current one and the specified journal value

Usage

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insert(Value request) throws FaultException {
    try {/*from   w ww . ja  va 2  s .com*/
        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:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value insertMany(Value request) throws FaultException {
    Value v = Value.create();//  w  ww.  j a v  a 2  s .  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:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value delete(Value request) throws FaultException {
    try {/* w  ww  .  j  a v  a 2 s  .co m*/
        Value v = Value.create();
        String collectionName = request.getFirstChild("collection").strValue();
        BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
        prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
        printlnJson("Delete filter", bsonQueryDocument);
        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);
        }

        DeleteResult resultDelete = db.getCollection(collectionName, BsonDocument.class)
                .deleteOne(bsonQueryDocument);
        v.getNewChild("deleteCount").add(Value.create(resultDelete.getDeletedCount()));
        return v;
    } catch (MongoException ex) {
        throw new FaultException("MongoException", ex);
    } catch (JsonParseException ex) {
        throw new FaultException("JsonParseException", ex);
    }
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value deleteMany(Value request) throws FaultException {
    try {/*ww w .j a v  a  2 s  . c  om*/
        Value v = Value.create();
        String collectionName = request.getFirstChild("collection").strValue();
        BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
        prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
        printlnJson("Delete filter", bsonQueryDocument);
        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);
        }
        DeleteResult resultDelete = db.getCollection(collectionName, BsonDocument.class)
                .deleteMany(bsonQueryDocument);

        v.getNewChild("deletedCount").add(Value.create(resultDelete.getDeletedCount()));
        return v;
    } catch (MongoException ex) {
        throw new FaultException("MongoException", ex);
    } catch (JsonParseException ex) {
        throw new FaultException("JsonParseException", ex);
    }
}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse

public Value updateMany(Value request) throws FaultException {
    try {//  w  w w.ja  va 2  s . com
        String collectionName = request.getFirstChild("collection").strValue();
        Value v = Value.create();
        BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
        prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
        printlnJson("Update filter", bsonQueryDocument);
        BsonDocument bsonDocument = BsonDocument.parse(request.getFirstChild("documentUpdate").strValue());
        printlnJson("Update documentUpdate", bsonDocument);
        prepareBsonQueryData(bsonDocument, request.getFirstChild("documentUpdate"));
        printlnJson("Update documentUpdate", 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")) {
            UpdateOptions updateOptions = new UpdateOptions();
            updateOptions.upsert(request.getFirstChild("options").getFirstChild("upsert").boolValue());
            updateOptions.bypassDocumentValidation(
                    request.getFirstChild("options").getFirstChild("bypassDocumentValidation").boolValue());
            UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class)
                    .updateMany(bsonQueryDocument, bsonDocument, updateOptions);
            v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount()));
            v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount()));

        } else {
            UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class)
                    .updateMany(bsonQueryDocument, bsonDocument);
            v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount()));
            v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount()));
        }

        return v;

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

}

From source file:joliex.mongodb.MongoDbConnector.java

@RequestResponse
public Value update(Value request) throws FaultException {
    try {//www  .  j  a va 2s .c o  m
        Value v = Value.create();
        String collectionName = request.getFirstChild("collection").strValue();
        BsonDocument bsonQueryDocument = BsonDocument.parse(request.getFirstChild("filter").strValue());
        prepareBsonQueryData(bsonQueryDocument, request.getFirstChild("filter"));
        printlnJson("Update filter", bsonQueryDocument);
        BsonDocument bsonDocument = BsonDocument.parse(request.getFirstChild("documentUpdate").strValue());
        printlnJson("Update documentUpdate", bsonDocument);
        prepareBsonQueryData(bsonDocument, request.getFirstChild("documentUpdate"));
        printlnJson("Update documentUpdate", bsonDocument);
        showLog();

        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")) {
            UpdateOptions updateOptions = new UpdateOptions();
            updateOptions.upsert(request.getFirstChild("options").getFirstChild("upsert").boolValue());
            updateOptions.bypassDocumentValidation(
                    request.getFirstChild("options").getFirstChild("bypassDocumentValidation").boolValue());
            UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class)
                    .updateOne(bsonQueryDocument, bsonDocument, updateOptions);
            v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount()));
            v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount()));

        } else {
            UpdateResult resultUpdate = db.getCollection(collectionName, BsonDocument.class)
                    .updateOne(bsonQueryDocument, bsonDocument);
            v.getNewChild("matchedCount").assignValue(Value.create(resultUpdate.getMatchedCount()));
            v.getNewChild("modifiedCount").assignValue(Value.create(resultUpdate.getModifiedCount()));
        }
        return v;

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

}