首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在mongodb的条件语句中使用元素匹配?

如何在mongodb的条件语句中使用元素匹配?
EN

Stack Overflow用户
提问于 2019-12-26 14:43:42
回答 2查看 202关注 0票数 0

我有两个订单

订单A:

代码语言:javascript
运行
复制
{
    "_id": "5e0092e725473011f4b4da54",
    "cash": 400.0,
    "deliveryFee": 5.0,
    "paymentOnPickUp" : false,
    "project": {
        "_id": "5dd3b4737908cb0cf7b40f84",
        "name": "Default Instant Deliveries"
    },
    "tasks": [{
            "tripType": "PICKUP",
            "status": "UNASSIGNED"
        },
        {
            "tripType": "DELIVERY",
            "status": "UNASSIGNED"
        }
    ]
}

订单B:

代码语言:javascript
运行
复制
{
    "_id": "5e0092e725473011f4b4da54",
    "cash": 250.0,
    "deliveryFee": 15.0,
    "paymentOnPickUp" : true,
    "project": {
        "_id": "5dd3b4737908cb0cf7b40f84",
        "name": "Default Instant Deliveries"
    },
    "tasks": [{
            "tripType": "PICKUP",
            "status": "FAILED"
        },
        {
            "tripType": "DELIVERY",
            "status": "UNASSIGNED"
        }
    ]
}

现在我必须获得基于项目的聚合信息。但我只需要添加现金,如果在pickUp上的任务没有失败。

订单A的现金收款为400,订单B的收款为800,订单B的提货任务失败。所以我们不需要加现金,但要加运费。

代码语言:javascript
运行
复制
| Project                    | Cash | Delivery Fee | total Orders |
|----------------------------|------|--------------|--------------|
| Default Instant Deliveries | 400  | 20           | 2            |

下面是我尝试过的事情

代码语言:javascript
运行
复制
fun getProjectsSummary(): MutableList<OrderSummaryDto> {

        val collectionPriceCondition = ConditionalOperators.`when`(where("tasks")
                .elemMatch(where("status").`is`("FAILED"))).then(0.0).otherwiseValueOf("cash")


        val agg = Aggregation.newAggregation(
                Aggregation.project()
                        .and("cashCollection").applyCondition(collectionPriceCondition),
                Aggregation.group("project.id")
                        .sum("deliveryFee").`as`("totalDeliveryPrice")
                        .sum("cashCollection").`as`("totalCashCollection")
                        .first("project.name").`as`("projectName")
                        .count().`as`("totalOrders")
        )
        return mongoTemplate
                .aggregate(agg, "ordersCollection", OrderSummaryDto::class.java).mappedResults
    }

但是我的身体状况不好,不能使用elematch。

谁能解释一下我们如何实现这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-31 02:09:23

$elemMatch仅适用于match阶段。对于任何其他阶段,您必须使用ArrayOperators。您可以使用$in数组运算符。

将条件转换为在运算符中使用(使用java代码)

代码语言:javascript
运行
复制
AggregationExpression collectionPriceCondition = ConditionalOperators.
                      when(ArrayOperators.arrayOf("tasks.status").containsValue("FAILED")).
                      then(0.0).
                      otherwiseValueOf("cash");
票数 2
EN

Stack Overflow用户

发布于 2019-12-30 14:19:46

你试过这个吗..。

代码语言:javascript
运行
复制
List<Criteria> criteriaList = new ArrayList<>();
criteriaList.add(where("tasks.status").`is`("FAILED"));
criteriaList.add(where("tasks.status").`nin`("COMPLETED"));
val collectionPriceCondition = ConditionalOperators.`when`(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()])))).then(0.0).otherwiseValueOf("cash")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59484792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档