简体   繁体   English

Flask-MongoEngine和PyMongo聚合查询

[英]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. 我正在尝试使用flask-mongoengine进行聚合查询,从我所看到的内容听起来并不可能。

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. 我查看了几个论坛主题,电子邮件链和Stack Overflow上的一些问题,但我还没有找到一个如何用flask-mongoengine实现聚合的一个很好的例子。

There is a comment in this question that says you have to use "raw pymongo and aggregation functionality." 这个问题中有一条评论说你必须使用“原始pymongo和聚合功能”。 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. 我已经修改了Python并使用Flask框架进行了基本的应用程序,但是深入研究完整的应用程序以及连接/查询Mongo对我来说是一个新手。

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? 有人可以提供一个示例(或示例的链接),我可以如何利用我的flask-mongoengine模型,但是使用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)? 这需要两个连接到MongoDB(一个用于PyMongo执行聚合查询,另一个用于通过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): 我想要执行的聚合查询的示例如下(此查询获取了我在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 }

The class your define with Mongoengine actually has a _get_collection() method which gets the "raw" collection object as implemented in the pymongo driver. 您使用Mongoengine定义的类实际上有一个_get_collection()方法,该方法获取pymongo驱动程序中实现的“原始”集合对象。

I'm just using the name Model here as a placeholder for your actual class defined for the connection in this example: 我只是在这里使用名称Model作为占位符,为此示例中为连接定义的实际类:

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. 因此,您始终可以访问pymongo对象而无需建立单独的连接。 Mongoengine is itself build upon pymongo. Mongoengine本身就是以pymongo为基础的。

aggregate is available since Mongoengine 0.9. aggregate可自Mongoengine 0.9。 Link to the API Reference . 链接到API参考

As there is no example whatsoever around, here is how you perform an aggregate query using aggregation framework with Mongoengine > 0.9 由于没有任何示例,以下是使用Mongoengine> 0.9的聚合框架执行聚合查询的方法

pipeline = [
  { '$group' : 
    { '_id' : { 'carrier' : '$carrierA', 'category' : '$category' }, 
      'count' : { '$sum' : 1 }
    }
  }]

Model.objects().aggregate(*pipeline)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM