PyMongo Aggregation&Quot;AttributeError:'dict'对象没有属性'_TXN_READ_PERPERY'&QOT; [英] PyMongo Aggregation "AttributeError: 'dict' object has no attribute '_txn_read_preference'"

查看:31
本文介绍了PyMongo Aggregation&Quot;AttributeError:'dict'对象没有属性'_TXN_READ_PERPERY'&QOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我的代码中有错误,因为我是pyMongo的新手,但我会试一试。MongoDB中的数据为167k+,如下所示:

{'overall': 5.0,
 'reviewText': {'ago': 1,
                'buy': 2,
                'daughter': 1,
                'holiday': 1,
                'love': 2,
                'niece': 1,
                'one': 2,
                'still': 1,
                'today': 1,
                'use': 1,
                'year': 1},
 'reviewerName': 'dcrm'}
我希望获得所有5.0评级字段中使用术语的计数。我运行了下面的代码,得到了下面的错误。有什么见解吗?

#1 Find the top 20 most common words found in 1-star reviews.

aggr = [{"$unwind": "$reviewText"}, 
        {"$group": { "_id": "$reviewText", "word_freq": {"$sum":1}}}, 
        {"$sort": {"word_freq": -1}},
        {"$limit": 20},
        {"$project": {"overall":"$overall", "word_freq":1}}]
disk_use = { 'allowDiskUse': True }
findings = list(collection.aggregate(aggr, disk_use))

for item in findings:
    p(item)

如您所见,我遇到了‘allDiskUse’组件,因为我似乎超过了100MB阈值。但是我得到的错误是:

AttributeError: 'dict' object has no attribute '_txn_read_preference'

推荐答案

您非常接近,allowDiskUse被命名为参数而不是字典,因此语句应如下所示

findings = list(collection.aggregate(aggr, allowDiskUse=True))

findings = list(collection.aggregate(aggr, **disk_use ))

这篇关于PyMongo Aggregation&Quot;AttributeError:'dict'对象没有属性'_TXN_READ_PERPERY'&QOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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