jeudi 9 avril 2015

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

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?


Aucun commentaire:

Enregistrer un commentaire