Pymongo:bson.errors.InvalidDocument:无法编码对象:<pymongo.cursor.cursor object =“”at =“”0xc61990 =“”> </pymongo.cursor.cursor>

时间:2014-02-01 08:08:16

标签: python mongodb mongodb-query pymongo twython

我正在尝试使用Pymongo将字典插入到MongoDB集合中。代码段是

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

所有变量名称都有有效值。但是我在执行时遇到了这个错误:

/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>

我不明白这意味着什么,有人可以帮忙吗?

编辑: -

print mydict正在展示

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

所以我尝试将pymongo游标更改为列表,新的回溯是:

代码: -

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)

新的追溯是: -

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

1 个答案:

答案 0 :(得分:4)

您似乎已经回答了您的初步问题,只是偶然发现了另一个问题:

不应该这样:

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

是这样的:

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