title: MongoDb aggregate the count/sum of 3 fields
body: <p>Im trying to get a sum of content a user has liked, shared or commented.</p>
  
  <p>my content model has the following structure.</p>
  
  <pre><code>Content = {
    _id: ObjectId(),
    author: ObjectId(),
    value: &lt;string of post content&gt;
    tags: [&lt;trings&gt;],
    likes: [array of user ids],
    shares: [array of user ids],
    comments: [{
       ContentId( of the same schema ),
       User Id
    }]
  }
  </code></pre>
  
  <p>Now, id like to aggregate over the content documents, 
  and get a result like this.</p>
  
  <pre><code>{
     likes: 20,
     shares: 10,
     comments: 5
  }
  </code></pre>
  
  <p>So, in short, whenever there is a content, 
  where user id is in likes array, 
  likes gets incremented by 1.</p>
  
  <p>Same for shares and comments. </p>
  
  <p>Im not sure if this is possible with aggregation framework.
  I think not, but maybe some mongodb gurus know better</p>
  
  <p>Quick Edit. Partially based on first post submitted, i made this.
  Now, it seems to work, but im sure there is some sort of gotcha, that im missing, since it seems too simple :)</p>
  
  <pre><code>db.getCollection('contents').aggregate( [
  
  
  {$facet: {
        "likes": [
           {$match: {likes: ObjectId("596537d6b63edc2318ee9f0c")} },
           {$group: {
             _id : "$likes",
             count: { $sum: 1 }
           }},
           { $project: { count: 1} }
        ],
        "shares": [
           {$match: {shares: ObjectId("596537d6b63edc2318ee9f0c")} },
           {$group: {
             _id : "$shares",
             count: { $sum: 1 }
           }},
           { $project: { count: 1} }
        ],
        "posts": [
           {$match: {$and: [
               {user: ObjectId("596537d6b63edc2318ee9f0c")},
               {parent: {$exists: false} }
           ]} },
           {$group: {
             _id : "$_id",
             count: { $sum: 1 }
           }},
           { $project: { count: 1} }
        ]
    }
  }]);
  </code></pre>
  
  <p>Can you spot something wrong with the code above ?</p>