Search in sources :

Example 1 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeFindOneAndUpdate.

OperationResult executeFindOneAndUpdate(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter").asDocument();
    BsonValue update = arguments.get("update");
    ClientSession session = getSession(arguments);
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "filter":
            case "update":
            case "session":
                break;
            case "returnDocument":
                switch(cur.getValue().asString().getValue()) {
                    case "Before":
                        options.returnDocument(ReturnDocument.BEFORE);
                        break;
                    case "After":
                        options.returnDocument(ReturnDocument.AFTER);
                        break;
                    default:
                        throw new UnsupportedOperationException("Can't happen");
                }
                break;
            case "hint":
                if (cur.getValue().isString()) {
                    options.hintString(cur.getValue().asString().getValue());
                } else {
                    options.hint(cur.getValue().asDocument());
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            return update.isArray() ? collection.findOneAndUpdate(filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.findOneAndUpdate(filter, update.asDocument(), options);
        } else {
            return update.isArray() ? collection.findOneAndUpdate(session, filter, update.asArray().stream().map(BsonValue::asDocument).collect(toList()), options) : collection.findOneAndUpdate(session, filter, update.asDocument(), options);
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 2 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeRunCommand.

public OperationResult executeRunCommand(final BsonDocument operation) {
    MongoDatabase database = entities.getDatabase(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
    ClientSession session = getSession(arguments);
    BsonDocument command = arguments.getDocument("command");
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "command":
            case "commandName":
            case "session":
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            return database.runCommand(command, BsonDocument.class);
        } else {
            return database.runCommand(session, command, BsonDocument.class);
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString) Map(java.util.Map) MongoDatabase(com.mongodb.client.MongoDatabase) BsonValue(org.bson.BsonValue)

Example 3 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeDeleteOne.

OperationResult executeDeleteOne(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter");
    ClientSession session = getSession(arguments);
    DeleteOptions options = getDeleteOptions(arguments);
    return resultOf(() -> {
        if (session == null) {
            return toExpected(collection.deleteOne(filter, options));
        } else {
            return toExpected(collection.deleteOne(session, filter, options));
        }
    });
}
Also used : DeleteOptions(com.mongodb.client.model.DeleteOptions) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession)

Example 4 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeInsertMany.

OperationResult executeInsertMany(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    List<BsonDocument> documents = arguments.getArray("documents").stream().map(BsonValue::asDocument).collect(toList());
    ClientSession session = getSession(arguments);
    InsertManyOptions options = new InsertManyOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "documents":
            case "session":
                break;
            case "ordered":
                options.ordered(cur.getValue().asBoolean().getValue());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            return toExpected(collection.insertMany(documents, options));
        } else {
            return toExpected(collection.insertMany(session, documents, options));
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString) Map(java.util.Map) InsertManyOptions(com.mongodb.client.model.InsertManyOptions) BsonValue(org.bson.BsonValue)

Example 5 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeCreateIndex.

public OperationResult executeCreateIndex(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument keys = arguments.getDocument("keys").asDocument();
    ClientSession session = getSession(arguments);
    IndexOptions options = new IndexOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "keys":
            case "session":
                break;
            case "name":
                options.name(cur.getValue().asString().getValue());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            collection.createIndex(keys, options);
        } else {
            collection.createIndex(session, keys, options);
        }
        return null;
    });
}
Also used : BsonDocument(org.bson.BsonDocument) IndexOptions(com.mongodb.client.model.IndexOptions) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Aggregations

ClientSession (com.mongodb.client.ClientSession)52 BsonDocument (org.bson.BsonDocument)19 BsonValue (org.bson.BsonValue)17 BsonString (org.bson.BsonString)16 Map (java.util.Map)15 Document (org.bson.Document)12 Test (org.junit.jupiter.api.Test)10 BsonArray (org.bson.BsonArray)6 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)4 TransactionOptions (com.mongodb.TransactionOptions)3 MongoClient (com.mongodb.client.MongoClient)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 Test (org.junit.Test)3 Point (org.springframework.data.geo.Point)3 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)3 DeleteOptions (com.mongodb.client.model.DeleteOptions)2 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 NonNull (com.mongodb.lang.NonNull)2