last day (15 days later) » 

3:11 PM
0
A: MongoDB Aggregation pipeline problems

chridamSince the basket field is an array, you need to flatten it using $unwind before running the $group aggregate operation. Also, create a new field in the $project that holds the exposure before the $group pipeline. Continuing from your previous attempt, you could try the following pipeline: db.str...

 
Thank you very much for this. I'll give it a go and report back. Clearly I need to read up about the unwind stage.
I think that has worked however it looks like the calculations for gross and net exposure are being downcast to integers and so I can't see the result as the exposure as % of aum is always < 1.0. Is there a way of forcing a type on the outcome?
 
Somehow I'm failing to grasp the formulas, is it (sum(shares*price*fx))/aum or sum((shares*price*fx)/aum)? For more details of the arithmetic operators, refer to the docs
 
The former (sum(sharespricefx))/aum
 
I've updated my answer, can you try again?
 
The query runs and the data is flattened (which is perfect) but the calculated numbers are null. I'll have to sit and look at what you are doing as you are very close.
 
3:11 PM
I think I know why, the expression"$multiply": ["$basket.shares", "$basket.price", "$basket.fx"] might be the culprit since it's different from the one you have. To debug a pipeline that's giving you unxepected results, run the aggregation query at each pipeline stage, test whether the results tally for each step. So for example, in the aggregation above, I first run db.strategy.aggregate([ { "$unwind": "$basket" }], check the result to see if the basket array is deconstructed properly, add the next pipeline stage, run that and repeat the steps till you get to the final step.
 
I will do. Thanks for your help. I thought mongodb would be easy enough if understood SQL but it's clearly more complicated than I expected :(
 
No worries, you will soon get the hang of it :)
 
OK. I am running the pipeline in stages
Stage 1 ($unwind) looks good as does stage 2 (your example didn't carry the AUM over in the projection so I added it). The aum value and the exposure values are correct.
The next stage (3 $group) appears to go awry. Total exposure is a � under MongoChef (presumably this means NaN) and the aum that is selected is incorrect. I need to select the aum associated with the current group (i.e. associated with the date/time/ strategy I'm looking at). $first is picking an AUM up but I think its from somewhere else in the data not the current group
Hope you don't mind me using you as a sounding board by the way
 
4:11 PM
LOL, not at all
I can see you are no getting the hang of it
 

  last day (15 days later) »