[英]Pymongo aggregate does not return a cursor but an object
我正在使用pymongo编写代码,它使用聚合框架将一些数据保存在其他集合中。 代码是这样的:
from pymongo import MongoClient
def makeAggregate():
print 'Making aggregation of commits..'
commitsCollection = MongoClient("mongo-srv", 27017).gt.commits
rankingCollection = MongoClient("mongo-srv", 27017).gt.commitsRanking
pipe = [{'$unwind': '$commits'},{'$group':{"_id":"$_id", "picture": {"$first": "$picture"},'a':{'$sum':'$commits.a'},'d':{'$sum':'$commits.d'},'c':{'$sum':'$commits.c'}}}]
cursor = commitsCollection.aggregate(pipeline=pipe)
obj = next(cursor, None)
while obj:
rankingCollection.save(obj)
obj = next(cursor, None)
makeAggregate()
代码在我的计算机上工作正常,但是当我将脚本移动到服务器时,脚本失败了,说:
Traceback (most recent call last):
File "aggregate.py", line 17, in <module>
makeAggregate()
File "aggregate.py", line 12, in makeAggregate
obj = next(cursor, None)
TypeError: dict object is not an iterator
命令python --version
返回
在我的电脑上: Python 2.7.3
在服务器上的Python 2.7.6
命令pip show pymongo
返回
在我的电脑上:
Usage: pip COMMAND [OPTIONS]
pip: error: No command by the name pip show
(maybe you meant "pip install show")
(已执行pip install show
但在运行show时仍然这样说..)
在服务器上:
Name: pymongo
Version: 2.7
Location: /usr/local/lib/python2.7/dist-packages/pymongo-2.7-py2.7-linux-x86_64.egg
Requires:
在python中运行pymongo.version
给了我:
在我的电脑中: 3.0
在服务器2.7
也许我必须更新这个? 我怎样才能做到这一点?
是的,这就是问题,
不同版本的Pymongo用于开发和生产环境
在PyMongo 2.7中它返回: Dictionary
{u'ok': 1.0, u'result': [{u'count': 3, u'_id': u'cat'}, {u'count': 2, u'_id': u'dog'}, {u'count': 1, u'_id': u'mouse'}]}
而在PyMongo 3.0中它返回: Cursor Object
{u'count': 3, u'_id': u'cat'}, {u'count': 2, u'_id': u'dog'}, {u'count': 1, u'_id': u'mouse'}
请参阅Pymongo 2.7文档
请参阅Pymongo 3.0文档
从PyMongo 2.7到PyMongo 3.0的更改
专业提示:使用Python的虚拟环境并创建需求文本文件。因为您可以安装相同版本的Python库及其在本地开发和生产中的依赖关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.