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();
}
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()));
}
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);
}
Aggregations