我确信我的代码中有一个错误,因为我是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评级在reviewText
字段中使用的术语的总数。我已经运行了以下代码,并得到了以下错误。有什么见解吗?
#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 ))
ManishSingh的回应是最好的,但如果你不明白他的确切意思以及你为什么会出现这个错误,我可以澄清你有什么以及为什么这是不正确的:
问题可能是您正在使用";allowDiskUse";报价如下:
findings = list(collection.aggregate(aggr, {"allowDiskUse": True})) # wrong
但正确的做法是:
findings = list(collection.aggregate(aggr, allowDiskUse=True)) # correct