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);
}
});
}
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);
}
});
}
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));
}
});
}
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));
}
});
}
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;
});
}
Aggregations