use of com.mongodb.client.model.FindOneAndUpdateOptions in project engine by Lumeer.
the class MongoDataDao method patchData.
@Override
public DataDocument patchData(final String collectionId, final String documentId, final DataDocument data) {
data.remove(ID);
Document updateDocument = new Document("$set", new Document(data));
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
Document patchedDocument = dataCollection(collectionId).findOneAndUpdate(idFilter(documentId), updateDocument, options);
if (patchedDocument == null) {
throw new StorageException("Document '" + documentId + "' has not been patched (partially updated).");
}
return MongoUtils.convertDocument(patchedDocument);
}
use of com.mongodb.client.model.FindOneAndUpdateOptions 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.model.FindOneAndUpdateOptions in project mongo-java-driver by mongodb.
the class MongoCollectionImplTest method testFindOneAndUpdate.
@Test
public void testFindOneAndUpdate() {
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().collation(collation);
Document update = new Document();
assertAll("findOneAndUpdate", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndUpdate(null, update)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndUpdate(filter, (Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndUpdate(clientSession, null, update)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndUpdate(clientSession, filter, update, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndUpdate(null, filter, update)), () -> assertThrows(IllegalArgumentException.class, () -> collection.findOneAndUpdate(null, filter, update, options))), () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndUpdate(null, filter, update, new FindOneAndUpdateOptions());
assertPublisherIsTheSameAs(expected, collection.findOneAndUpdate(filter, update), "Default");
}, () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndUpdate(null, filter, update, options);
assertPublisherIsTheSameAs(expected, collection.findOneAndUpdate(filter, update, options), "With filter & options");
}, () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndUpdate(clientSession, filter, update, new FindOneAndUpdateOptions());
assertPublisherIsTheSameAs(expected, collection.findOneAndUpdate(clientSession, filter, update), "With client session");
}, () -> {
Publisher<Document> expected = mongoOperationPublisher.findOneAndUpdate(clientSession, filter, update, options);
assertPublisherIsTheSameAs(expected, collection.findOneAndUpdate(clientSession, filter, update, options), "With client session, filter & options");
});
}
use of com.mongodb.client.model.FindOneAndUpdateOptions in project spring-data-mongodb by spring-projects.
the class MongoTemplateUnitTests method findAndModfiyShoudUseCollationWhenPresent.
// DATAMONGO-1518
@Test
void findAndModfiyShoudUseCollationWhenPresent() {
template.findAndModify(new BasicQuery("{}").collation(Collation.of("fr")), new Update(), AutogenerateableId.class);
ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
use of com.mongodb.client.model.FindOneAndUpdateOptions in project spring-data-mongodb by spring-projects.
the class MongoTemplateUnitTests method findAndModfiyShoudUseDefaultCollationWhenPresent.
// DATAMONGO-1854
@Test
void findAndModfiyShoudUseDefaultCollationWhenPresent() {
template.findAndModify(new BasicQuery("{}"), new Update(), Sith.class);
ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
assertThat(options.getValue().getCollation()).isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build());
}
Aggregations