use of com.mongodb.client.model.ReplaceOptions in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeReplaceOne.
OperationResult executeReplaceOne(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
BsonDocument filter = arguments.getDocument("filter");
BsonDocument replacement = arguments.getDocument("replacement");
ReplaceOptions options = getReplaceOptions(arguments);
return resultOf(() -> toExpected(collection.replaceOne(filter, replacement, options)));
}
use of com.mongodb.client.model.ReplaceOptions in project mongo-java-driver by mongodb.
the class MongoCollectionImplTest method testReplaceOne.
@Test
public void testReplaceOne() {
ReplaceOptions options = new ReplaceOptions().collation(collation);
Document replacement = new Document();
assertAll("replaceOne", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.replaceOne(null, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.replaceOne(filter, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.replaceOne(clientSession, null, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.replaceOne(clientSession, filter, replacement, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.replaceOne(null, filter, replacement)), () -> assertThrows(IllegalArgumentException.class, () -> collection.replaceOne(null, filter, replacement, options))), () -> {
Publisher<UpdateResult> expected = mongoOperationPublisher.replaceOne(null, filter, replacement, new ReplaceOptions());
assertPublisherIsTheSameAs(expected, collection.replaceOne(filter, replacement), "Default");
}, () -> {
Publisher<UpdateResult> expected = mongoOperationPublisher.replaceOne(null, filter, replacement, options);
assertPublisherIsTheSameAs(expected, collection.replaceOne(filter, replacement, options), "With filter & options");
}, () -> {
Publisher<UpdateResult> expected = mongoOperationPublisher.replaceOne(clientSession, filter, replacement, new ReplaceOptions());
assertPublisherIsTheSameAs(expected, collection.replaceOne(clientSession, filter, replacement), "With client session");
}, () -> {
Publisher<UpdateResult> expected = mongoOperationPublisher.replaceOne(clientSession, filter, replacement, options);
assertPublisherIsTheSameAs(expected, collection.replaceOne(clientSession, filter, replacement, options), "With client session, filter & options");
});
}
use of com.mongodb.client.model.ReplaceOptions in project immutables by immutables.
the class MongoSession method update.
/**
* Uses <a href="https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/">replaceOne</a> operation
* with <a href="https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/">bulkWrite</a>. Right now has to convert
* object to BsonDocument to extract {@code _id} attribute.
*/
private <T> Publisher<WriteResult> update(StandardOperations.Update operation) {
ReplaceOptions options = new ReplaceOptions();
if (operation.upsert()) {
options.upsert(operation.upsert());
}
List<ReplaceOneModel<Object>> docs = operation.values().stream().map(value -> new ReplaceOneModel<>(new BsonDocument(Mongos.ID_FIELD_NAME, toBsonValue(keyExtractor.extract(value))), value, options)).collect(Collectors.toList());
Publisher<BulkWriteResult> publisher = ((MongoCollection<Object>) collection).bulkWrite(docs);
return Flowable.fromPublisher(publisher).map(x -> WriteResult.unknown());
}
use of com.mongodb.client.model.ReplaceOptions in project spring-data-mongodb by spring-projects.
the class MongoTemplateUnitTests method replaceOneShouldUseCollationWhenPresent.
// DATAMONGO-1518
@Test
void replaceOneShouldUseCollationWhenPresent() {
template.updateFirst(new BasicQuery("{}").collation(Collation.of("fr")), new Update(), AutogenerateableId.class);
ArgumentCaptor<ReplaceOptions> options = ArgumentCaptor.forClass(ReplaceOptions.class);
verify(collection).replaceOne(any(), any(), options.capture());
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
use of com.mongodb.client.model.ReplaceOptions in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method replaceOneShouldUseCollationWhenPresent.
// DATAMONGO-1518
@Test
void replaceOneShouldUseCollationWhenPresent() {
when(collection.replaceOne(any(Bson.class), any(), any(ReplaceOptions.class))).thenReturn(Mono.empty());
template.updateFirst(new BasicQuery("{}").collation(Collation.of("fr")), new Update(), AutogenerateableId.class).subscribe();
ArgumentCaptor<ReplaceOptions> options = ArgumentCaptor.forClass(ReplaceOptions.class);
verify(collection).replaceOne(any(Bson.class), any(), options.capture());
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Aggregations