mongodb aggregate group by category
<p>I am new in using MongoDB's aggregation framework and here I have below a schema:</p>

<pre><code>var bookSoldSchema = new Schema({
   buyer:{
      type: Number
   }
   book: {         
     type: Schema.Types.ObjectId, ref: 'Book'
   } 
});

 var bookSchema = new Schema({
     bookName: { type: String },
     categories: [{ type: Schema.Types.ObjectId, ref: 'BookCategory'}],
 })
</code></pre>

<p>Each book has multiple categories, I would like to show the top 5 best seller categories and each category I need show top 3 books which where sold most, and I also need show the category name, the results which I need is:</p>

<pre><code>[
      {
        categoryId: xxx,
        categoryName:xxx,
        top3books:[
           {bookId:xxx, bookName:xxx},
           {bookId:xxx, bookName:xxx},
           {bookId:xxx, bookName:xxx},
        ]
     },
     {
       categoryId: xxx,
       categoryName:xxx,
       top3books:[
          {bookId:xxx, bookName:xxx},
          {bookId:xxx, bookName:xxx},
          {bookId:xxx, bookName:xxx},
       ]
    }
]
</code></pre>

<p>How can I go about this in MongoDB? </p>