Search in sources :

Example 26 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project spring-data-mongodb by spring-projects.

the class ExecutableUpdateOperationSupportTests method updateAll.

// DATAMONGO-1563
@Test
public void updateAll() {
    UpdateResult result = template.update(Person.class).apply(new Update().set("firstname", "Han")).all();
    assertThat(result.getModifiedCount()).isEqualTo(2L);
    assertThat(result.getUpsertedId()).isNull();
}
Also used : Update(org.springframework.data.mongodb.core.query.Update) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 27 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method testWriteConcernResolver.

@Test
public void testWriteConcernResolver() {
    PersonWithIdPropertyOfTypeObjectId person = new PersonWithIdPropertyOfTypeObjectId();
    person.setId(new ObjectId());
    person.setFirstName("Dave");
    template.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
    template.save(person);
    UpdateResult result = template.updateFirst(query(where("id").is(person.getId())), update("firstName", "Carter"), PersonWithIdPropertyOfTypeObjectId.class);
    FsyncSafeWriteConcernResolver resolver = new FsyncSafeWriteConcernResolver();
    template.setWriteConcernResolver(resolver);
    Query q = query(where("_id").is(person.getId()));
    Update u = update("firstName", "Carter");
    result = template.updateFirst(q, u, PersonWithIdPropertyOfTypeObjectId.class);
    MongoAction lastMongoAction = resolver.getMongoAction();
    assertThat(lastMongoAction.getCollectionName(), is("personWithIdPropertyOfTypeObjectId"));
    assertThat(lastMongoAction.getDefaultWriteConcern(), equalTo(WriteConcern.UNACKNOWLEDGED));
    assertThat(lastMongoAction.getDocument(), notNullValue());
    assertThat(lastMongoAction.getEntityType().toString(), is(PersonWithIdPropertyOfTypeObjectId.class.toString()));
    assertThat(lastMongoAction.getMongoActionOperation(), is(MongoActionOperation.UPDATE));
    assertThat(lastMongoAction.getQuery(), equalTo(q.getQueryObject()));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) ObjectId(org.bson.types.ObjectId) Update(org.springframework.data.mongodb.core.query.Update) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 28 with UpdateResult

use of com.mongodb.client.result.UpdateResult in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method beforeConvertEventForUpdateSeesNextVersion.

// DATAMONGO-1639
@Test
public void beforeConvertEventForUpdateSeesNextVersion() {
    final VersionedEntity entity = new VersionedEntity();
    entity.id = 1;
    entity.version = 0;
    GenericApplicationContext context = new GenericApplicationContext();
    context.refresh();
    context.addApplicationListener(new AbstractMongoEventListener<VersionedEntity>() {

        @Override
        public void onBeforeConvert(BeforeConvertEvent<VersionedEntity> event) {
            assertThat(event.getSource().version, is(1));
        }
    });
    template.setApplicationContext(context);
    MongoTemplate spy = Mockito.spy(template);
    UpdateResult result = mock(UpdateResult.class);
    doReturn(1L).when(result).getModifiedCount();
    doReturn(result).when(spy).doUpdate(anyString(), Mockito.any(Query.class), Mockito.any(Update.class), Mockito.any(Class.class), anyBoolean(), anyBoolean());
    spy.save(entity);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)28 Test (org.junit.Test)17 Document (org.bson.Document)13 Update (org.springframework.data.mongodb.core.query.Update)10 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 Query (org.springframework.data.mongodb.core.query.Query)5 Formatter (java.util.Formatter)4 Bson (org.bson.conversions.Bson)4 BasicDBObject (com.mongodb.BasicDBObject)3 DBObject (com.mongodb.DBObject)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)3 DeleteResult (com.mongodb.client.result.DeleteResult)3 List (java.util.List)3 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)3 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)3 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 HashMap (java.util.HashMap)2 BsonDocument (org.bson.BsonDocument)2