python 调用mongo aggregate函数 提示错误:pymongo.errors.OperationFailure: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.
本地环境:
python2.7_32
MongoDB version:3.2.5
报错原因:
mongo内存限制。 aggregate函数 使用$group时,数据大小必须小于16945KB。
解决方法:
添加设置:allowDiskUse:true
在mongo中修改,如下例:
db.stocks.aggregate( [
{ $project : { cusip: 1, date: 1, price: 1, _id: 0 } },
{ $sort : { cusip : 1, date: 1 } }
],
{ allowDiskUse: true }
)
在python中修改,如下例:(两种方式)
方式一:
pipeline = [
{"$group": {"_id": {"uid": "$uid"}, "number": {"$sum": 1}}}]
options = {
'allowDiskUse':
True}
queryList = rawTable.aggregate(pipeline, **options)
方式二:
queryList = rawTable.aggregate([{"$group": {"_id": {"uid": "$uid"}, "number": {"$sum": 1}}}], allowDiskUse=True)
注意:方式一中,参数options,前需加“**”,否则报--> TypeError: aggregate() takes exactly 2 arguments (3 given)
参考mongo中文社区:http://docs.mongoing.com/manual-zh/reference/command/aggregate.html#dbcmd.aggregate
转载请注明原文地址: https://ju.6miu.com/read-675084.html