Pymongo component does not return a cursor, but an object

I am writing code using pymongo that uses an aggregation structure to store some data in another collection. The code looks like 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 the server, 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

Command python --versionreturns

On my computer: Python 2.7.3

On server Python 2.7.6

Command pip show pymongoreturns

On my computer:

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

(Done pip install show, but continues to talk about it when running show ..)

On 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.versioninside python gives me:

In my computer: 3.0

On server 2.7

Maybe I need to update this? How can i do this?

+4
source share
1 answer

, ,
Pymongo

PyMongo 2.7 :

{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 :

{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

Pro-Tip: Python . Python .

Python

+7

Source: https://habr.com/ru/post/1609995/


All Articles