Spring Aggregation Framework: How to create nested $divide, $subtract, $mod query?

假如想象 提交于 2019-12-11 13:52:10

问题


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.

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}
  }
 }
 ]);

I started like this:

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

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

Basically, I have to subtract:

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

from

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

Any idea how to solve this?


回答1:


Use SpEL like:

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



来源:https://stackoverflow.com/questions/29535332/spring-aggregation-framework-how-to-create-nested-divide-subtract-mod-quer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!