Search in sources :

Example 1 with Update

use of org.springframework.data.mongodb.core.query.Update in project books by aidanwhiteley.

the class UserRepositoryImpl method updateUserRoles.

@Override
public long updateUserRoles(ClientRoles clientRoles) {
    List<User.Role> roles = new ArrayList<>();
    if (clientRoles.isAdmin()) {
        roles.add(User.Role.ROLE_ADMIN);
    }
    if (clientRoles.isEditor()) {
        roles.add(User.Role.ROLE_EDITOR);
    }
    Query query = new Query(Criteria.where("id").is(clientRoles.getId()));
    Update update = new Update();
    update.set("roles", roles);
    UpdateResult result = mongoTemplate.updateFirst(query, update, User.class);
    // noinspection ConstantConditions
    if (result != null) {
        return result.getModifiedCount();
    } else {
        return 0;
    }
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) ArrayList(java.util.ArrayList) Update(org.springframework.data.mongodb.core.query.Update) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 2 with Update

use of org.springframework.data.mongodb.core.query.Update in project commons-dao by reportportal.

the class ProjectRepositoryCustomImpl method removeUserFromProjects.

@Override
public void removeUserFromProjects(String userId) {
    Query query = Query.query(Criteria.where(USER_LOGIN).is(userId));
    mongoTemplate.updateMulti(query, new Update().pull("users", new BasicDBObject("login", userId)), Project.class);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 3 with Update

use of org.springframework.data.mongodb.core.query.Update in project commons-dao by reportportal.

the class TestItemRepositoryCustomImpl method updateItemsIssues.

@Override
public void updateItemsIssues(Map<String, TestItemIssue> forUpdate) {
    Query query = query(where(ID).in(forUpdate.keySet()));
    Update update = new Update();
    mongoTemplate.stream(query, TestItem.class).forEachRemaining(dbo -> {
        String currentId = dbo.getId();
        TestItemIssue newValue = forUpdate.get(currentId);
        update.set(ISSUE_TYPE, newValue.getIssueType());
        update.set(ISSUE_DESCRIPTION, newValue.getIssueDescription());
        update.set(ISSUE_TICKET, newValue.getExternalSystemIssues());
        update.set(ISSUE_ANALYZED, newValue.isAutoAnalyzed());
        mongoTemplate.updateFirst(Query.query(Criteria.where(ID).is(currentId)), update, mongoTemplate.getCollectionName(TestItem.class));
    });
}
Also used : TestItemIssue(com.epam.ta.reportportal.database.entity.item.issue.TestItemIssue) Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update) TestItem(com.epam.ta.reportportal.database.entity.item.TestItem)

Example 4 with Update

use of org.springframework.data.mongodb.core.query.Update in project commons-dao by reportportal.

the class UserPreferenceRepositoryCustomImpl method deleteUnsharedFilters.

@Override
public void deleteUnsharedFilters(String username, String project, String filterId) {
    Query query = Query.query(where("userRef").ne(username)).addCriteria(where("projectRef").is(project)).addCriteria(where("launchTabs.filters").is(filterId));
    Update update = new Update().pull("launchTabs.filters", filterId);
    mongoTemplate.updateMulti(query, update, UserPreference.class);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 5 with Update

use of org.springframework.data.mongodb.core.query.Update in project spring-data-mongodb by spring-projects.

the class MongoTemplate method doSaveVersioned.

private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
    ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
    MongoPersistentProperty property = entity.getRequiredVersionProperty();
    Number number = convertingAccessor.getProperty(property, Number.class);
    if (number != null) {
        // Bump version number
        convertingAccessor.setProperty(property, number.longValue() + 1);
        maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
        assertUpdateableIdIfNotSet(objectToSave);
        Document document = new Document();
        this.mongoConverter.write(objectToSave, document);
        maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
        Update update = Update.fromDocument(document, ID_FIELD);
        // Create query for entity with the id and old version
        MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
        Object id = entity.getIdentifierAccessor(objectToSave).getRequiredIdentifier();
        Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
        UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
        if (result.getModifiedCount() == 0) {
            throw new OptimisticLockingFailureException(String.format("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?", id, number, collectionName));
        }
        maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
        return objectToSave;
    }
    doInsert(collectionName, objectToSave, this.mongoConverter);
    return objectToSave;
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) Update(org.springframework.data.mongodb.core.query.Update) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) UpdateResult(com.mongodb.client.result.UpdateResult)

Aggregations

Update (org.springframework.data.mongodb.core.query.Update)211 Test (org.junit.jupiter.api.Test)179 Document (org.bson.Document)117 Query (org.springframework.data.mongodb.core.query.Query)71 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)57 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)23 Bson (org.bson.conversions.Bson)21 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)15 ObjectId (org.bson.types.ObjectId)12 UpdateResult (com.mongodb.client.result.UpdateResult)11 List (java.util.List)8 ToString (lombok.ToString)8 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)8 Criteria (org.springframework.data.mongodb.core.query.Criteria)8 VersionedPerson (org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson)7 StepVerifier (reactor.test.StepVerifier)7 BsonDocument (org.bson.BsonDocument)6 UpdateOptions (com.mongodb.client.model.UpdateOptions)5 ArrayList (java.util.ArrayList)5 Instant (java.time.Instant)4