How To Use Group By In PyMongo ( Python + MongoDB)

Praveen Singh         No comments

I have been covering Group By since my last two posts.This time I will tell you , how to run the same using Python. There is a specific driver to use MongoDB with Python , known as PyMongo.We will use this driver to for any operation on MongoDB.

I am going to use the same collection which I had created in my last post. I am using PyCharm as my IDE. If you are interested , look for my post in which I had already explained how to install this IDE.

Enough talk, come to the point now...

I am performing the same operation here.Selecting the Minimum age value from the dataset.Our programme will look like this:

Some simple steps here:

from pymongo import MongoClient : This will always be the first step.
conn = MongoClient()          : connection string
db =                      : connecting info database
col =                         : connecting info collection ( !! same name of db and collection)
print col.aggregate([{"$group": {"_id":"0", "minAge": {"$min":"$Age"}}}])
                                            : simply printing the minimum value

Published by Praveen Singh

A blogger by passion.You can find me tucked in my bed and blogging on weekends when not roaming around. Besides blogging, I love music and you can find my songs on my fb page:PraveenUnplugged
Follow on Youtube : Videos On Latest Happenings |ThingsToKnow
Follow us Talend In Action

0 responses:

© 2015 Techie's House. Designed by Bloggertheme9