Search in sources :

Example 21 with UpdateOptions

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

the class MongoCollectionImplTest method testUpdateMany.

@Test
public void testUpdateMany() {
    UpdateOptions options = new UpdateOptions().collation(collation);
    List<Document> updates = singletonList(new Document());
    assertAll("updateMany", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.updateMany(null, updates)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateMany(filter, (Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateMany(clientSession, null, updates)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateMany(clientSession, filter, updates, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateMany(null, filter, updates)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateMany(null, filter, updates, options))), () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateMany(null, filter, updates, new UpdateOptions());
        assertPublisherIsTheSameAs(expected, collection.updateMany(filter, updates), "Default");
    }, () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateMany(null, filter, updates, options);
        assertPublisherIsTheSameAs(expected, collection.updateMany(filter, updates, options), "With filter & options");
    }, () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateMany(clientSession, filter, updates, new UpdateOptions());
        assertPublisherIsTheSameAs(expected, collection.updateMany(clientSession, filter, updates), "With client session");
    }, () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateMany(clientSession, filter, updates, options);
        assertPublisherIsTheSameAs(expected, collection.updateMany(clientSession, filter, updates, options), "With client session, filter & options");
    });
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 22 with UpdateOptions

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

the class MongoCollectionImplTest method testUpdateOne.

@Test
public void testUpdateOne() {
    UpdateOptions options = new UpdateOptions().collation(collation);
    Document update = new Document();
    assertAll("updateOne", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.updateOne(null, update)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateOne(filter, (Bson) null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateOne(clientSession, null, update)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateOne(clientSession, filter, update, null)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateOne(null, filter, update)), () -> assertThrows(IllegalArgumentException.class, () -> collection.updateOne(null, filter, update, options))), () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateOne(null, filter, update, new UpdateOptions());
        assertPublisherIsTheSameAs(expected, collection.updateOne(filter, update), "Default");
    }, () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateOne(null, filter, update, options);
        assertPublisherIsTheSameAs(expected, collection.updateOne(filter, update, options), "With filter & options");
    }, () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateOne(clientSession, filter, update, new UpdateOptions());
        assertPublisherIsTheSameAs(expected, collection.updateOne(clientSession, filter, update), "With client session");
    }, () -> {
        Publisher<UpdateResult> expected = mongoOperationPublisher.updateOne(clientSession, filter, update, options);
        assertPublisherIsTheSameAs(expected, collection.updateOne(clientSession, filter, update, options), "With client session, filter & options");
    });
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 23 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project java-design-patterns by iluwatar.

the class MongoBank method setFunds.

@Override
public void setFunds(String bankAccount, int amount) {
    var search = new Document("_id", bankAccount);
    var update = new Document("_id", bankAccount).append("funds", amount);
    var updateOptions = new UpdateOptions().upsert(true);
    accountsCollection.updateOne(search, new Document("$set", update), updateOptions);
}
Also used : Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 24 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method updateFirstShouldPreferExplicitCollationOverDefaultCollation.

// DATAMONGO-1854
@Test
void updateFirstShouldPreferExplicitCollationOverDefaultCollation() {
    template.updateFirst(new BasicQuery("{}").collation(Collation.of("fr")), Update.update("foo", "bar"), Sith.class).subscribe();
    ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
    verify(collection).updateOne(any(), any(Bson.class), options.capture());
    assertThat(options.getValue().getCollation()).isEqualTo(com.mongodb.client.model.Collation.builder().locale("fr").build());
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 25 with UpdateOptions

use of com.mongodb.client.model.UpdateOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method updateOneShouldUseCollationWhenPresent.

// DATAMONGO-1518
@Test
void updateOneShouldUseCollationWhenPresent() {
    when(collection.updateOne(any(Bson.class), any(Bson.class), any())).thenReturn(Mono.empty());
    template.updateFirst(new BasicQuery("{}").collation(Collation.of("fr")), new Update().set("foo", "bar"), AutogenerateableId.class).subscribe();
    ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
    verify(collection).updateOne(any(), any(Bson.class), options.capture());
    assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AutogenerateableId(org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId) Update(org.springframework.data.mongodb.core.query.Update) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Aggregations

UpdateOptions (com.mongodb.client.model.UpdateOptions)53 Document (org.bson.Document)31 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)21 Bson (org.bson.conversions.Bson)17 UpdateResult (com.mongodb.client.result.UpdateResult)10 Test (org.junit.jupiter.api.Test)10 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)8 ArrayList (java.util.ArrayList)6 BsonDocument (org.bson.BsonDocument)6 Test (org.junit.Test)6 Update (org.springframework.data.mongodb.core.query.Update)4 BasicDBObject (com.mongodb.BasicDBObject)3 MongoCollection (com.mongodb.client.MongoCollection)3 CountOptions (com.mongodb.client.model.CountOptions)3 IOException (java.io.IOException)3 List (java.util.List)3 BsonValue (org.bson.BsonValue)3 ObjectId (org.bson.types.ObjectId)3 MongodbException (com.duangframework.core.exceptions.MongodbException)2 Block (com.mongodb.Block)2