Before we start learning about aggregation in MongoDB, we are going to need some sample data in the first place to perform aggregation on. We will populate a collection with some dummy data. Each document in the collection will have the following fields:
title:
A string, represents the title of the articledescription:
Content of the article, also a stringauthor:
A string representing the name of the authorcategory:
Category of the articlerating:
An integer between 1 and 10tags:
An array, contains between 1 to 5 distinct tags of the articlepublished_at:
A timestamp
A sample document from the collection will look similar to the following:
{ "_id" : ObjectId("4dfb49545981ae0a02680700"), "title" : "Programmers will act rational when all other possibilities have been exhausted.", "author" : "Spock", "category" : "Programming", "rating" : 6, "tags" : ["security", "code","howto"], "published_at" : ISODate("2011-06-13T12:32:20Z") }