Flask-MongoEngine &PyMongo 聚合查询

Flask-MongoEngine amp; PyMongo Aggregation Query(Flask-MongoEngine amp;PyMongo 聚合查询)
本文介绍了Flask-MongoEngine &PyMongo 聚合查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用flask-mongoengine 进行聚合查询,根据我的阅读,这听起来不太可能.

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.

我查看了几个论坛主题、电子邮件链和一些关于 Stack Overflow 的问题,但我还没有找到一个很好的例子来说明如何使用 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.

这个问题中有一条评论说你必须使用raw pymongo and聚合功能."但是,没有例子说明它是如何工作的.我已经修改了 Python 并使用 Flask 框架构建了一个基本应用程序,但我正在研究完整的应用程序和应用程序.连接/查询 Mongo 对我来说很新鲜.

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.

有人可以提供一个示例(或指向示例的链接),说明我可以如何利用我的 flask-mongoengine 模型,但使用 PyMongo 的聚合框架进行查询?这是否需要两个到 MongoDB 的连接(一个用于 PyMongo 执行聚合查询,另一个用于通过 MongoEngine 进行常规查询/插入/更新)?

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)?

我想要执行的聚合查询示例如下(这个查询让我得到了我在 Mongo shell 中想要的信息):

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 }
        }
    }
])

此查询的输出示例:

{ "_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 }

推荐答案

你用 Mongoengine 定义的类实际上有一个 _get_collection() 方法,它获取在 pymongo 中实现的原始"集合对象司机.

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

我只是在这里使用名称 Model 作为此示例中为连接定义的实际类的占位符:

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 }
        }
    }
])

因此,您始终可以访问 pymongo 对象,而无需建立单独的连接.Mongoengine 本身是基于 pymongo 构建的.

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

这篇关于Flask-MongoEngine &PyMongo 聚合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)