Spring 聚合框架:如何创建嵌套的 $divide、$subtract、$mod 查询? [英] Spring Aggregation Framework: How to create nested $divide, $subtract, $mod query?

查看:27
本文介绍了Spring 聚合框架:如何创建嵌套的 $divide、$subtract、$mod 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Java Spring 聚合框架中创建一个 ProjectionOperation.以下简化的原始 mongo db 查询工作正常,应使用 java 代码实现.

I'm trying to create a ProjectionOperation in the Java Spring aggregation framework. The following simplified raw mongo db query works fine and should be implemented with java code.

我正在为查询的 $project 部分的第一行苦苦挣扎.如何在聚合框架中编写嵌套项目查询?(旁注:startTime 是固定且给定的 unix 时间戳)

I'm struggling with the first row in the $project section of my query. How can I write nested project queries in the aggregation framework? (side note: startTime is a fixed and given unix timestamp)

db.procedure_executions.aggregate([
{ $match : { 
        "application.$id" : ObjectId("55144929bc26f48fb5de15a7")
    }
},
{ $project : { 
    'startTimeGrouping' : { '$subtract' : [ { $divide : ["$startTime", 3600 ]}, { $mod : [{ $divide : ["$startTime", 3600 ]},1] } ] },
    'caller': "$procedure.annotations.members.caller",
    'callee': "$procedure.annotations.members.callee"
    } 
},
{ $group : {
    _id : { 
        startTimeGrouping : "$startTimeGrouping", 
        caller: "$caller",         
        callee: "$callee" 
    }, 
    'count' : { '$sum' : 1}
  }
 }
 ]);

我是这样开始的:

final ProjectionOperation projectionOperation = project()
         .and(CALLER).as(CALLER_ATTRIBUTE)
         .and(CALLEE).as(CALLEE_ATTRIBUTE)
...       

但我不知道如何嵌套多个 $divide、$mod、$subtract 等.

but I have no idea how to nest multiple $divide, $mod, $subtract, etc.

基本上,我必须减去:

.and('startTime').divide(3600).mod(1)

来自

.and('startTime').divide(3600)

知道如何解决这个问题吗?

Any idea how to solve this?

推荐答案

像这样使用 SpEL:

Use SpEL like:

.andExpression("(startTime/3600) - ((startTime/3600) % 1")

这篇关于Spring 聚合框架:如何创建嵌套的 $divide、$subtract、$mod 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆