DEV Community 👩‍💻👨‍💻

Claudio Fior for Abbrevia

Posted on

The beautiful marriage of Python and MongoDB

I'm a PHP developer and managing big data stored with this language is not so easy.

Add data to the db is not a problem, but retrieving a summary and save it as csv is not to easy.

So this time I tried to use Python and it marvelous because:

  • In a moment you can create the object containing the aggregate conditions;
  • Saving the data as csv is easy.
#!/usr/bin/python
import pymongo
import csv
import collections
from pymongo import MongoClient
db = MongoClient().report_impresa
pipeline = [
    {
        "$group": {
            "_id": "$CompanySummary.CreditRating.CreditLimit",
            "count": {"$sum": 1},
            "min_rating": {"$min": "$CompanySummary.CreditRating.CommonValue"},
            "max_rating": {"$max": "$CompanySummary.CreditRating.CommonValue"},
            }
    },
    {"$sort": {"_id": 1}}
]
descr = db.ggs.aggregate(pipeline)
values = {}
for att in descr:
    chiave = att["_id"]
    if chiave is None:
        chiave = ""
    if chiave == "":
        values[""] = att
    else:
        values[int(chiave)] = att

with open('fido.csv', 'w') as csvfile:
    writer = csv.DictWriter(
                            csvfile,
                            fieldnames=[
                                "_id",
                                "count",
                                "min_rating",
                                "max_rating"
                                ]
                            )
    writer.writeheader()
    for k in sorted(values):
        writer.writerow(values[k])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Hacktoberfest is happening now!


It is a month-long celebration of open source. For a lot of devs, its their introduction to open source.



Check out the Hacktoberfest tag on DEV to keep up with the latest!