使用聚合在Spring Data MongoDB中进行更新

问题描述 投票:0回答:1

在Spring Data MongoDB 更新查询中是否可以使用aggregation pipeline

假设,对于没有firstName的用户,我想在fullName字段中设置lastName值。本机查询如下所示:

db.getCollection("users").update({lastName: ""}, [{ $set: { "fullName" : "$firstName" } }])

使用Spring Data是否可行?

spring mongodb spring-data-mongodb
1个回答
0
投票

[AggregationUpdate将成为Spring Data MongoDB 3.0的一部分。

AggregationUpdate update = AggregationUpdate.update()
    .set("average").toValue(ArithmeticOperators.valueOf("tests").avg())
    .set("grade").toValue(
        ConditionalOperators.switchCases(
            CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(90)).then("A"),
            CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(80)).then("B"),
            CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(70)).then("C"),
            CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(60)).then("D"))
        .defaultTo("F"));
© www.soinside.com 2019 - 2024. All rights reserved.