1

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 Answer 1

1

Use SpEL like:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.