MongoDB Aggregation pipeline problems
<p>I'm new to mongoDB and am having difficulty getting my head around aggregation pipelines.</p>

<p>I have created a database that holds information regarding my stock trading. In a cut down version one document from my portfolio collection looks a bit like this</p>

<pre><code>   {
   "date" : 2015-12-31T15:50:00.000Z,
   "time" : 1550,
   "aum"  : 1000000,
   "basket" :[
   {
     "_id" : "Microsoft",
     "shares" : 10,
     "price"  : 56.53,
     "fx"     : 1.0
   },
   .
   .
   .
   {
     "_id" : "GOOG.N",
     "shares" : 20,
     "price"  : 759.69,
     "fx"     : 1.0
   }
</code></pre>

<p>]</p>

<p>So, for each day, I keep track of my assets under management (aum) and a list of all the positions I hold with the current price. What I need to do is to calculate the daily net and gross exposure for the portfolio as a percentage of aum. Net exposure is simply:</p>

<p><code>sum(shares*price*fx)/aum</code> </p>

<p>over all the stocks. Gross exposure is: </p>

<p><code>abs(shares*price*fx)/aum</code> </p>

<p>(a negative position means a short position). I need to do this as a single query using the aggregation framework. I have tried numbers of queries but none seem to work so clearly I'm just wandering around in the dark. Can anyone give some guidance?</p>

<p>My query looks like this</p>

<pre><code>db.strategy.aggregate(

  // Pipeline
  [
    // Stage 1
    {
      $project: {
        "_id": 0,
        "date":1,
        "time":1,
        "aum":1,
        "strategyName":1,
        "gExposure": {$divide: ["$grossExposure","$aum"]}
      }
    },

    // Stage 2
    {
      $group: {
        _id :{ date:"$date",time:"$time",strategyName:"$strategyName"},
        grossExposure: { $sum: { $abs: {$multiply: [ "$basket.sysCurShares","$basket.price","$basket.fx" ] } }}
      }
    },

    // Stage 3
    {
      $sort: {
      "_id.date": 1,  "_id.time": 1,  "_id.strategyName": 1 
      }
    }

  ]
);
</code></pre>

<p>The query runs but the calculated value is zero. My projection isn't working as I'd expect either as I would like all the data flattened to a two dimensional table.</p>