DEV Community

Claudio Fior for Abbrevia

Posted on

4 3

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay