【问题标题】:spring mongodb aggregation compare two field and get sum of one columnspring mongodb聚合比较两个字段并获得一列的总和
【发布时间】:2017-05-05 16:42:20
【问题描述】:

我有一个订单集合,orderLastStatusChangeDatetime、estimatedDeliveryDatetime 和 orderPrice 是订单集合的归档名称。我必须得到 orderPrice 的总和,其中 orderLastStatusChangeDatetime 小于或等于 estimatedDeliveryDatetime 。我已经使用下面的查询来获取总记录...

Criteria criteria = new Criteria() {
        @Override
        public DBObject getCriteriaObject() {
            DBObject obj = new BasicDBObject();
            obj.put("$where", "this.orderLastStatusChangeDatetime <= this.estimatedDeliveryDatetime");
            return obj;
        }
    };

    Query query = new Query();


    query.addCriteria(criteria);


    totalOrder = (int) mongoTemplate.count(query,ORDERS_COLLECTION_NAME);

但我必须得到订单价格的总和。我在聚合匹配中使用了相同的标准。但它给出错误“命令失败,错误 16395:'异常:$where 不允许在 $match 聚合表达式中'”

【问题讨论】:

    标签: spring mongodb spring-data-mongodb


    【解决方案1】:

    您可以使用以下聚合管道。在$project 阶段创建一个cmp 字段来保存orderLastStatusChangeDatetime &lt;= estimatedDeliveryDatetime 的结果,然后是$matchcmp 等于true$group$sum order price

    import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
    import static org.springframework.data.mongodb.core.query.Criteria.where;
    
    Aggregation aggregation = newAggregation(project("orderPrice").andExpression("orderLastStatusChangeDatetime <= estimatedDeliveryDatetime").as("cmp"), match(Criteria.where("cmp").is(true)), group().sum("orderPrice").as("total"));
    
    BasicDBObject results = mongoOperations.aggregate(aggregation, ORDERS_COLLECTION_NAME, BasicDBObject.class).getUniqueMappedResult();
    int totalOrder = results.getInt("total");
    

    更新:在 1.8.5 RELEASE 中使用 AggregationExpression

    Aggregation agg = newAggregation(
        project("orderPrice").and(new AggregationExpression() {
                    @Override
                    public DBObject toDbObject(AggregationOperationContext context) {
                        return new BasicDBObject("$lte", Arrays.<Object>asList("$orderLastStatusChangeDatetime", "$estimatedDeliveryDatetime"));
                        }
        }).as("cmp"),
        match(Criteria.where("cmp").is(true)),
        group().sum("orderPrice").as("total")
    );
    

    【讨论】:

    • 感谢您的快速帮助 Veeram...但我遇到了错误 ---
    • java.lang.IllegalArgumentException: Unsupported Element: org.springframework.data.mongodb.core.spel.OperatorNode@542dba3f Type: class org.springframework.data.mongodb.core.spel.OperatorNode 你可能您的 SpEL 表达式中有语法错误!
    • 不客气。我能够在 1.10.1 spring mongo 版本上成功运行它。你能告诉我代码和你使用的是什么版本吗?这是我在服务器上看到的查询{ "$project" : { "orderPrice" : 1 , "cmp" : { "$lte" : [ "$orderLastStatusChangeDatetime" , "$estimatedDeliveryDatetime"]}}} , { "$match" : { "cmp" : true}} , { "$group" : { "_id" : null , "total" : { "$sum" : "$orderPrice"}}}
    • 查询我正在使用聚合 agg = newAggregation( project("orderPrice","orderLastStatusChangeDatetime","estimatedDeliveryDatetime").andExpression("orderLastStatusChangeDatetime
    • 好的,我看到了你的问题。表达式中不支持 lt 运算符。在 1.8.0 版本中。可以升级到 1.8.5 RELEASE 吗?我为 1.8.5 RELEASE 添加了解决方案
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多