简体   繁体   English

Pymongo聚合不返回游标而是返回对象

[英]Pymongo aggregate does not return a cursor but an object

I am writing a code using pymongo which uses the aggregation framework to save some data in other collection. 我正在使用pymongo编写代码,它使用聚合框架将一些数据保存在其他集合中。 The code is this: 代码是这样的:

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()

The code works fine on my computer, but when I moved the script to a server, then the script failed, saying: 代码在我的计算机上工作正常,但是当我将脚本移动到服务器时,脚本失败了,说:

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

The command python --version returns 命令python --version返回

On my computer: Python 2.7.3 在我的电脑上: Python 2.7.3

On the server Python 2.7.6 在服务器上的Python 2.7.6

The command pip show pymongo returns 命令pip show pymongo返回

On my computer: 在我的电脑上:

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

(Executed pip install show but keeps saying this when running show..) (已执行pip install show但在运行show时仍然这样说..)

On the server: 在服务器上:

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

Running pymongo.version inside python gives me: 在python中运行pymongo.version给了我:

In my computer: 3.0 在我的电脑中: 3.0

In the server 2.7 在服务器2.7

Maybe I have to update this? 也许我必须更新这个? How can I do that? 我怎样才能做到这一点?

Yes thats the issue, 是的,这就是问题,
Different version of Pymongo for Development and Production environment 不同版本的Pymongo用于开发和生产环境

In PyMongo 2.7 it returns : Dictionary 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'}]}

Whereas in PyMongo 3.0 it returns : Cursor Object 而在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'}

Refer Pymongo 2.7 Documentation 请参阅Pymongo 2.7文档
Refer Pymongo 3.0 Documentation 请参阅Pymongo 3.0文档
Changes made from PyMongo 2.7 to PyMongo 3.0 从PyMongo 2.7到PyMongo 3.0的更改

Pro-Tip : Use Virtual Environment for Python and create a Requirements Text File.So as you can install the same version of Python Library and it Dependencies in Local Development and in Production. 专业提示:使用Python的虚拟环境并创建需求文本文件。因为您可以安装相同版本的Python库及其在本地开发和生产中的依赖关系。

Refer Virtual Environment Python Package 请参阅虚拟环境Python包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM