簡體   English   中英

Pymongo聚合不返回游標而是返回對象

[英]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庫及其在本地開發和生產中的依賴關系。

請參閱虛擬環境Python包

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM