Search in sources :

Example 56 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 57 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)

Example 58 with UpdateResult

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());
    }
}
Also used : UpdateResult(com.mongodb.client.result.UpdateResult)

Example 59 with UpdateResult

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());
    }
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 60 with UpdateResult

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;
    }
}
Also used : Message(net.dv8tion.jda.core.entities.Message) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) MongoException(com.mongodb.MongoException)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)74 Document (org.bson.Document)31 Test (org.junit.Test)20 Bson (org.bson.conversions.Bson)16 Update (org.springframework.data.mongodb.core.query.Update)12 Test (org.testng.annotations.Test)11 UpdateOptions (com.mongodb.client.model.UpdateOptions)10 BasicDBObject (com.mongodb.BasicDBObject)9 FindOptions (dev.morphia.query.FindOptions)8 BsonDocument (org.bson.BsonDocument)8 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)7 DeleteResult (com.mongodb.client.result.DeleteResult)7 Query (org.springframework.data.mongodb.core.query.Query)7 ObjectId (org.bson.types.ObjectId)6 UpdateOptions (dev.morphia.UpdateOptions)5 ArrayList (java.util.ArrayList)5 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 DBObject (com.mongodb.DBObject)4 Formatter (java.util.Formatter)4