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);
}
use of com.mongodb.client.result.UpdateResult in project books by aidanwhiteley.
the class BookRepositoryImpl method addGoogleBookItemToBook.
@Override
public void addGoogleBookItemToBook(String bookId, Item item) {
Query query = new Query(Criteria.where("id").is(bookId));
Update update = new Update();
update.set("googleBookDetails", item);
UpdateResult result = mongoTemplate.updateFirst(query, update, Book.class);
if (result.getModifiedCount() != 1) {
LOGGER.error("Expected 1 update for googleBookDetails in a Book for bookId {} but saw {}", bookId, result.getModifiedCount());
}
}
use of com.mongodb.client.result.UpdateResult in project books by aidanwhiteley.
the class UserRepositoryImpl method updateUserAdminNotified.
@Override
public void updateUserAdminNotified(User user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update();
update.set("adminEmailedAboutSignup", true);
UpdateResult result = mongoTemplate.updateFirst(query, update, User.class);
if (result.getModifiedCount() != 1) {
LOGGER.error("Expected 1 update to adminEmailedAboutSignup for user {} but saw {}", user, result.getModifiedCount());
}
}
use of com.mongodb.client.result.UpdateResult in project Saber-Bot by notem.
the class EntryManager method updateEntry.
/**
* Update an entry with a new configuration
* All schedule entry parameters should be filled.
* The ID of the ScheduleEntry must not have been changed.
* @param se (ScheduleEntry) the new schedule entry object
* @return true if successful, otherwise false
*/
public boolean updateEntry(ScheduleEntry se, boolean sort) {
Message origMessage = se.getMessageObject();
if (origMessage == null)
return false;
// process expiration date
Date expire = null;
if (se.getExpire() != null) {
expire = Date.from(se.getExpire().toInstant());
}
// process deadline
Date deadline = null;
if (se.getDeadline() != null) {
deadline = Date.from(se.getDeadline().toInstant());
}
// generate event display message
Message message = MessageGenerator.generate(se);
// update message display
Date finalExpire = expire;
Date finalDeadline = deadline;
Message msg = MessageUtilities.editMsg(message, origMessage);
if (msg == null)
return false;
try {
String guildId = msg.getGuild().getId();
String channelId = msg.getChannel().getId();
// replace whole document
Document entryDocument = new Document("_id", se.getId()).append("title", se.getTitle()).append("start", Date.from(se.getStart().toInstant())).append("end", Date.from(se.getEnd().toInstant())).append("comments", se.getComments()).append("recurrence", se.getRepeat()).append("reminders", se.getReminders()).append("end_reminders", se.getEndReminders()).append("url", se.getTitleUrl()).append("hasStarted", se.hasStarted()).append("messageId", msg.getId()).append("channelId", channelId).append("googleId", se.getGoogleId()).append("rsvp_members", se.getRsvpMembers()).append("rsvp_limits", se.getRsvpLimits()).append("start_disabled", se.isQuietStart()).append("end_disabled", se.isQuietEnd()).append("reminders_disabled", se.isQuietRemind()).append("expire", finalExpire).append("orig_start", Date.from(se.getRecurrence().getOriginalStart().toInstant())).append("count", se.getRecurrence().getCount()).append("image", se.getImageUrl()).append("thumbnail", se.getThumbnailUrl()).append("deadline", finalDeadline).append("guildId", guildId).append("announcements", new ArrayList<>(se.getAnnouncements())).append("announcement_dates", se.getaDates()).append("announcement_times", se.getAnnouncementTimes()).append("announcement_messages", se.getAnnouncementMessages()).append("announcement_targets", se.getAnnouncementTargets()).append("location", se.getLocation());
UpdateResult res = Main.getDBDriver().getEventCollection().replaceOne(eq("_id", se.getId()), entryDocument);
// return false, might result in skipped announcement or other issues
if (!res.wasAcknowledged())
return false;
// auto-sort
autoSort(sort, channelId);
return true;
} catch (Exception e) {
Logging.exception(EntryManager.class, e);
return false;
}
}
Aggregations