Question

I am trying to insert a dictionary using Pymongo into a MongoDB collection. The code segment is

newdict = {'id': a, 'usr_id': i.get('user_id'), 'reach_value': mycount, 'initfc': initfc}
myid = reach.insert(newdict)

Where all the variable names have valid values. But I am encountering this error on execution:

/usr/lib64/python2.7/site-packages/pymongo/collection.py:362: RuntimeWarning: couldn't encode - reloading python modules and trying again. if you see this without getting an InvalidDocument exception please see http://api.mongodb.org/python/current/faq.html#does-pymongo-work-with-mod-wsgi
  self.database.connection)
Traceback (most recent call last):
  File "get_reach.py", line 46, in <module>
    myid = reach.insert(newdict)
  File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 362, in insert
    self.database.connection)
bson.errors.InvalidDocument: Cannot encode object: <pymongo.cursor.Cursor object at 0x18e9990>

I don't understand what this means, can anyone please help?

EDIT:--

print mydict was showing

{'usr_id': 89443197, 'initfc': <pymongo.cursor.Cursor object at 0x13c0990>, 'reach_value': 0, 'id': 429127873031831552L}

So I tried to change the pymongo cursor to a list, and the new traceback is:

Code:-

from pymongo import MongoClient
from twython import Twython
client = MongoClient()
db = client.PWSocial
reach = db.reach
tweets = db.tweets
id_list = [57947109, 183093247, 89443197, 431336956]
APP_KEY = 'xx'
APP_SECRET = 'xx'
OAUTH_TOKEN = 'xx'
OAUTH_TOKEN_SECRET = 'xx'
dict = tweets.find()
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
for i in dict:
    if i.get('user_id') in id_list:
        a = i.get('id')
        mycount = 0
        #print a
        hasrt = list(db.retweets.find({'id': a}))
        #print hasrt
        test = list(reach.find({'id': a}))
        if hasrt and test:
            mylist = (twitter.get_retweeters_ids(id = i.get('id')))
            print mylist
            for var in mylist:
                RTer = twitter.show_user(user_id = var)
                mycount  = mycount + RTer.get("followers_count")
                init = test.get('initfc')
            diff = list(db.follower_count.find({'id': i.get('user_id')}))  - init
            mycount = mycount + diff
            initfc = list(db.follower_count.find({'id': i.get('user_id')}))
            #initfc = initf 
        elif hasrt:
            mylist = twitter.get_retweeters_ids(id = a)
            for var in mylist:
                RTer = twitter.show_user(user_id = var)
                mycount  = count + RTer.get("followers_count")  
            mycount = mycount + list(db.follower_count.find({'id': i.get('user_id')})) 
            initfc = list(db.follower_count.find({'id': i.get('user_id')}))

        else:
            mycount = list(db.follower_count.find({'id': i.get('user_id')})) 
            initfc = list(db.follower_count.find({'id': i.get('user_id')}))
            #initfc = initf 
        if test:
            reach.update({'id': a}, {'$set': {{ 'initfc': initfc }, {'reach_value': mycount}}})
        else:
            newdict = {'id': a, 'usr_id': i.get('user_id'), 'reach_value': mycount, 'initfc': initfc}
            print newdict
            myid = reach.insert(newdict)

And the new traceback is:-

 File "get_reach.py", line 46, in <module>
    reach.update({'id': a}, {'$set': {{ 'initfc': initfc }, {'reach_value': mycount}}})
TypeError: unhashable type: 'dict'
Was it helpful?

Solution

You seem to have answered your initial question only to stumble onto another issue:

Shouldn't this:

{'$set': {{ 'initfc': initfc }, {'reach_value': mycount}}}

Be this:

{'$set': { 'initfc': initfc , 'reach_value': mycount }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top