Pymongo 聚合不是 return 游标而是对象

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 --versionreturns

在我的电脑上: Python 2.7.3

在服务器上 Python 2.7.6

命令pip show pymongoreturns

在我的电脑上:

Usage: pip COMMAND [OPTIONS]
pip: error: No command by the name pip show
  (maybe you meant "pip install show")

(已执行 pip install show 但在 运行 显示时一直这样说..)

在服务器上:

Name: pymongo
Version: 2.7
Location: /usr/local/lib/python2.7/dist-packages/pymongo-2.7-py2.7-linux-x86_64.egg
Requires:

运行 pymongo.version inside python 给我:

在我的电脑中: 3.0

在服务器中 2.7

也许我必须更新这个?我该怎么做?

是的,就是这个问题,
用于开发和生产环境的不同版本的 Pymongo

PyMongo 2.7 它 returns : 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 它 returns : 光标对象

{u'count': 3, u'_id': u'cat'}, {u'count': 2, u'_id': u'dog'}, {u'count': 1, u'_id': u'mouse'}

Refer Pymongo 2.7 Documentation
Refer Pymongo 3.0 Documentation
Changes made from PyMongo 2.7 to PyMongo 3.0

专业提示: 为 Python 使用虚拟环境并创建需求文本 File.So,因为您可以在本地开发和生产中安装相同版本的 Python 库及其依赖项。

Refer Virtual Environment Python Package