Search in sources :

Example 1 with ReplaceOptions

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)));
}
Also used : BsonDocument(org.bson.BsonDocument) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions)

Example 2 with ReplaceOptions

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");
    });
}
Also used : ReplaceOptions(com.mongodb.client.model.ReplaceOptions) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.jupiter.api.Test)

Example 3 with ReplaceOptions

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());
}
Also used : Document(org.bson.Document) Iterables(com.google.common.collect.Iterables) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) Path(org.immutables.criteria.expression.Path) PathNaming(org.immutables.criteria.backend.PathNaming) Projections(com.mongodb.client.model.Projections) StandardOperations(org.immutables.criteria.backend.StandardOperations) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) BsonDocumentWriter(org.bson.BsonDocumentWriter) DefaultResult(org.immutables.criteria.backend.DefaultResult) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) ExpressionNaming(org.immutables.criteria.backend.ExpressionNaming) Backend(org.immutables.criteria.backend.Backend) Function(java.util.function.Function) FlowableTransformer(io.reactivex.FlowableTransformer) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Query(org.immutables.criteria.expression.Query) Level(java.util.logging.Level) FullDocument(com.mongodb.client.model.changestream.FullDocument) Bson(org.bson.conversions.Bson) ChangeStreamPublisher(com.mongodb.reactivestreams.client.ChangeStreamPublisher) Flowable(io.reactivex.Flowable) EncoderContext(org.bson.codecs.EncoderContext) WatchEvent(org.immutables.criteria.backend.WatchEvent) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Collation(org.immutables.criteria.expression.Collation) ExpressionConverter(org.immutables.criteria.expression.ExpressionConverter) MongoException(com.mongodb.MongoException) Publisher(org.reactivestreams.Publisher) CodecRegistries(org.bson.codecs.configuration.CodecRegistries) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) KeyExtractor(org.immutables.criteria.backend.KeyExtractor) Visitors(org.immutables.criteria.expression.Visitors) List(java.util.List) Sorts(com.mongodb.client.model.Sorts) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) UniqueCachedNaming(org.immutables.criteria.backend.UniqueCachedNaming) FindPublisher(com.mongodb.reactivestreams.client.FindPublisher) Codec(org.bson.codecs.Codec) WriteResult(org.immutables.criteria.backend.WriteResult) Optional(java.util.Optional) BackendException(org.immutables.criteria.backend.BackendException) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) BsonDocument(org.bson.BsonDocument) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) BulkWriteResult(com.mongodb.bulk.BulkWriteResult)

Example 4 with ReplaceOptions

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");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) Update(org.springframework.data.mongodb.core.query.Update) Test(org.junit.jupiter.api.Test)

Example 5 with ReplaceOptions

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");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) AutogenerateableId(org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId) Update(org.springframework.data.mongodb.core.query.Update) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Aggregations

ReplaceOptions (com.mongodb.client.model.ReplaceOptions)6 FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)5 BsonDocument (org.bson.BsonDocument)3 Test (org.junit.jupiter.api.Test)3 UpdateResult (com.mongodb.client.result.UpdateResult)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 Update (org.springframework.data.mongodb.core.query.Update)2 Iterables (com.google.common.collect.Iterables)1 MongoException (com.mongodb.MongoException)1 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)1 Projections (com.mongodb.client.model.Projections)1 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)1 Sorts (com.mongodb.client.model.Sorts)1 FullDocument (com.mongodb.client.model.changestream.FullDocument)1 ChangeStreamPublisher (com.mongodb.reactivestreams.client.ChangeStreamPublisher)1 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)1 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)1 Flowable (io.reactivex.Flowable)1