title: Mongo query not giving exact results for aggregate function
body: <p>My mongo database contains a collection 'Shops' and the data is like below:</p>
  
  <pre><code> {
          "_id" : ObjectId("XXXX1b83d2b227XXXX"),
          "ShopId" : 435,
          "products" : [ 
              {
                  "productId" : "1234",
                  "productName" : "non veg",
                  "productCategory" : "meals",
                  "mrp" : "38",
              }, 
               {
                  "productId" : "5234",
                  "productName" : "non veg",
                  "productCategory" : "meals",
                  "mrp" : "38",
              }, 
               {
                  "productId" : "6234",
                  "productName" : "apple",
                  "productCategory" : "juice",
                  "mrp" : "38",
              }, 
               {
                  "productId" : "7234",
                  "productName" : "non veg",
                  "productCategory" : "biriyani",
                  "mrp" : "38",
              }, 
               {
                  "productId" : "8234",
                  "productName" : "non veg",
                  "productCategory" : "biriyani",
                  "mrp" : "38",
              } 
             ]
      }
  </code></pre>
  
  <p>There will be several shops in the collection having a list of products.</p>
  
  <p><strong>Expected Output</strong></p>
  
  <pre><code>   { "productList": [
        {
          "categoryname": "meals",
          "productcount": "2",
          "products": [
            {
              "productname": "Non-Veg"
            },
             {
              "productname": "Veg"
            }
          ]
        },
        {
          "categoryname": "juice",
          "productcount": "1",
          "products": [
            {
              "productname": "apple"
            }
          ]
        },{......}
       ]
  }
  </code></pre>
  
  <p>I tried it using 'async' method with 2 queries, but I didn't get the output correctly. I think it can be done in one query without using 'async'.</p>
  
  <p>My code follows, I think it's the wrong approach:</p>
  
  <pre><code>model.Shops.aggregate([
      {$match:{ShopId:435}},
      {$unwind:"$products"},
      {$limit:2},{$skip:0},
      {$group:{_id:{"productCategory":"$products.productCategory"}}}
  ],function (err, doc) {
          if  (doc!=null){
              var arr = [];
              async.each(doc, function(item,callback){
                  model.Shops.aggregate([
                      {"$unwind":"$products"},
                      {$match:{"ShopId":435,"products.productCategory":item._id.productCategory}},
                      {$limit:2},
                      {
                          $group: {
                              _id:null,
                              "products": {
                                  $push:{"productName":"$products.productName"}
                              }
                          }
                      }
                  ], function (err,doc) {
                      arr.push({"categoryname":item._id.productCategory,"products":doc.products});
                      callback(null);
                  });
              },function (err) {
                  res.json(arr);
          });
      }
  });
  </code></pre>