MongoDB不使用allowDiskUsage:True处理聚合 [英] MongoDB doesn't handle aggregation with allowDiskUsage:True

查看:62
本文介绍了MongoDB不使用allowDiskUsage:True处理聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据结构如下:

way: {
    _id:'9762264'
    node: ['253333910', '3304026514']
}

,我试图以各种方式计算节点出现的频率.这是我使用pymongo的代码:

and I'm trying to count the frequency of nodes' appearance in ways. Here is my code using pymongo:

node = db.way.aggregate([
    {'$unwind': '$node'},
    {
        '$group': {
            '_id': '$node',
            'appear_count': {'$sum': 1}
        }
    },
    {'$sort': {'appear_count': -1}},
    {'$limit': 10}
],
    {'allowDiskUse': True}
)

它将报告错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File ".../OSM Wrangling/explore.py", line 78, in most_passed_node
    {'allowDiskUse': True}
  File ".../pymongo/collection.py", line 2181, in aggregate
    **kwargs)
  File ".../pymongo/collection.py", line 2088, in _aggregate
    client=self.__database.client)
  File ".../pymongo/pool.py", line 464, in command
    self.validate_session(client, session)
  File ".../pymongo/pool.py", line 609, in validate_session
    if session._client is not client:
AttributeError: 'dict' object has no attribute '_client'

但是,如果我删除了 {'allowDiskUse':True} 并在较小的数据集上对其进行了测试,则效果很好.看来allowDiskUse语句带来了什么问题?而且在MongoDB的文档中没有有关此错误的信息

However, if I removed the {'allowDiskUse': True} and test it on a smaller set of data, it works well. It seems that the allowDiskUse statement brings something wrong? And there is no information about this mistake in the docs of MongoDB

我应该如何解决这个问题并获得想要的答案?

How should I solve this problem and get the answer I want?

推荐答案

我应该如何解决这个问题并获得想要的答案?

How should I solve this problem and get the answer I want?

这是因为在PyMongo v3.6中, collection.aggregate()已更改.添加了 session 的可选参数.现在,方法签名为:

This is because in PyMongo v3.6 the method signature for collection.aggregate() has been changed. An optional parameter for session has been added. The method signature now is :

aggregate(pipeline, session=None, **kwargs)

将此应用于您的代码示例,您可以如下指定 allowDiskUse :

Applying this to your code example, you can specify allowDiskUse as below:

node = db.way.aggregate(pipeline=[
                {'$unwind': '$node'},
                {'$group': {
                          '_id': '$node',
                          'appear_count': {'$sum': 1}
                          }
                 },
                 {'$sort': {'appear_count': -1}},
                 {'$limit': 10}
               ],
               allowDiskUse=True
        )

如果愿意,另请参见 pymongo.client_session 了解有关 session 的更多信息.

See also pymongo.client_session if you would like to know more about session.

这篇关于MongoDB不使用allowDiskUsage:True处理聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆