I am writing a code using pymongo which uses the aggregation framework to save some data in other collection. 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
On my computer:
Python 2.7.3
On the server
Python 2.7.6
The command pip show pymongo
returns
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..)
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:
In my computer:
3.0
In the server
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
In PyMongo 2.7 it 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'}]}
Whereas in PyMongo 3.0 it returns : 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
Refer Pymongo 3.0 Documentation
Changes made from PyMongo 2.7 to 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.
Refer Virtual Environment Python Package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With