Python MongoDB – $group (aggregation)

10 Jul, 2020
MongoDB is an open-source document-oriented database. MongoDB stores data in the form of key-value pairs and is a NoSQL database program. The term NoSQL means non-relational. In this article, we will see the use of $group in MongoDB using Python.
In PyMongo, the Aggregate Method is mainly used to process data records from multiple documents and returns the result to the user. This is based on the data processing pipeline and includes multiple stages at the end of which we get the aggregated result. One of the stages of the aggregate method includes $group. This operation groups input documents of the collection by the specified identifier expression entered by the user and then apply the accumulator expression to it. Then it produces the output documents. 
$group includes the following-
_id- The documents are grouped according to the given id expression.field (Optional) – It includes accumulator expression which is applied to the included documents.
_id- The documents are grouped according to the given id expression.
field (Optional) – It includes accumulator expression which is applied to the included documents.
Let’s understand this through some examples.
Example 1:
Python3
from pymongo import MongoClient     # creation of MongoClient client=MongoClient()     # Connect with the portnumber and host client = MongoClient("mongodb://localhost:27017/")     # Access database mydatabase = client['database']     # Access collection of the database mycollection=mydatabase['myTable'] writer_profiles = [    {"_id":1, "user":"Amit", "title":"Python", "comments":5},    {"_id":2, "user":"Drew",  "title":"JavaScript", "comments":15},    {"_id":3, "user":"Amit",  "title":"C++", "comments":6},    {"_id":4, "user":"Drew",  "title":"MongoDB", "comments":2},    {"_id":5, "user":"Cody",  "title":"Perl", "comments":9}]  mycollection.insert_many(writer_profiles)agg_result= mycollection.aggregate(    [{    "$group" :         {"_id" : "$user",          "num_tutorial" : {"$sum" : 1}         }}    ])for i in agg_result:    print(i)
Output:
{'_id': 'Cody', 'num_tutorial': 1}
{'_id': 'Drew', 'num_tutorial': 2}
{'_id': 'Amit', 'num_tutorial': 2}

In the above example, the documents are grouped on the basis of expression $user, and then the field num_tutorial includes the accumulator operator $sum that calculates the number of tutorials of each user. 
Example 2:
Python3
from pymongo import MongoClient     # creation of MongoClient client=MongoClient()     # Connect with the portnumber and host client = MongoClient("mongodb://localhost:27017/")     # Access database mydatabase = client['database4']     # Access collection of the database mycollection=mydatabase['myTable'] writer_profiles = [    {"_id":1, "user":"Amit", "title":"Python", "comments":8},    {"_id":2, "user":"Drew",  "title":"JavaScript", "comments":15},    {"_id":3, "user":"Amit",  "title":"C++", "comments":6},    {"_id":4, "user":"Drew",  "title":"MongoDB", "comments":2},    {"_id":5, "user":"Cody",  "title":"MongoDB", "comments":16}]  mycollection.insert_many(writer_profiles)agg_result= mycollection.aggregate(    [{    "$group" :         {"_id" : "$title",           "total" : {"$sum" : 1}         }}    ])for i in agg_result:    print(i)
Output:
{'_id': 'MongoDB', 'total': 2}
{'_id': 'C++', 'total': 1}
{'_id': 'JavaScript', 'total': 1}
{'_id': 'Python', 'total': 1}
In this example, the documents are grouped by the expression $title, and the field total includes the accumulator operator $sum that calculates the number of articles of each title. 
Python-mongoDB
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.