Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-MongoEngine & PyMongo Aggregation Query

I am trying to make an aggregation query using flask-mongoengine, and from what I have read it does not sound like it is possible.

I have looked over several forum threads, e-mail chains and a few questions on Stack Overflow, but I have not found a really good example of how to implement aggregation with flask-mongoengine.

There is a comment in this question that says you have to use "raw pymongo and aggregation functionality." However, there is no examples of how that might work. I have tinkered with Python and have a basic application up using Flask framework, but delving into full fledged applications & connecting/querying to Mongo is pretty new to me.

Can someone provide an example (or link to an example) of how I might utilize my flask-mongoengine models, but query using the aggregation framework with PyMongo? Will this require two connections to MongoDB (one for PyMongo to perform the aggregation query, and a second for the regular query/insert/updating via MongoEngine)?

An example of the aggregation query I would like to perform is as follows (this query gets me exactly the information I want in the Mongo shell):

db.entry.aggregate([
    { '$group' : 
        { '_id' : { 'carrier' : '$carrierA', 'category' : '$category' }, 
          'count' : { '$sum' : 1 }
        }
    }
])

An example of the output from this query:

{ "_id" : { "carrier" : "Carrier 1", "category" : "XYZ" }, "count" : 2 }
{ "_id" : { "carrier" : "Carrier 1", "category" : "ABC" }, "count" : 4 }
{ "_id" : { "carrier" : "Carrier 2", "category" : "XYZ" }, "count" : 31 }
{ "_id" : { "carrier" : "Carrier 2", "category" : "ABC" }, "count" : 6 }
like image 296
SirCobalt Avatar asked Jun 25 '14 17:06

SirCobalt


People also ask

What is flask MongoEngine?

MongoEngine is an ODM (Object Document Mapper) that maps Python classes (models) to MongoDB documents, making it easy to create and manipulate documents programatically straight from our code.

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

What is the difference between PyMongo and MongoEngine?

PyMongo is the low-level driver wrapping the MongoDB API into Python and delivering JSON in and out. MongoEngine or other layers like MongoKit map your MongoDB-based data to objects similar to native Python database drivers + SQLAlchemy as ORM.

How do I update MongoEngine?

MongoEngine provides the following methods for atomic updates on a queryset. update_one() − Overwrites or adds first document matched by query. update() − Performs atomic update on fields matched by query. modify() − Update a document and return it.


1 Answers

The class your define with Mongoengine actually has a _get_collection() method which gets the "raw" collection object as implemented in the pymongo driver.

I'm just using the name Model here as a placeholder for your actual class defined for the connection in this example:

Model._get_collection().aggregate([
    { '$group' : 
        { '_id' : { 'carrier' : '$carrierA', 'category' : '$category' }, 
          'count' : { '$sum' : 1 }
        }
    }
])

So you can always access the pymongo objects without establishing a separate connection. Mongoengine is itself build upon pymongo.

like image 133
Neil Lunn Avatar answered Sep 21 '22 08:09

Neil Lunn