python - Pymongo:bson.errors.InvalidDocument:无法编码对象:<pymongo.cursor.Cursor object at 0xc61990>

标签 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游标改为列表,新的traceback为:

代码:-

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'

最佳答案

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

不应该这样:

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

是这样的:

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

关于python - Pymongo:bson.errors.InvalidDocument:无法编码对象:<pymongo.cursor.Cursor object at 0xc61990>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495707/

相关文章:

python - 可靠地发送有关 Celery 任务成功/失败的邮件

python - 将列表 A 中的项目追加到列表 B(如果列表 B 中尚未包含这些项目)

python - 使用 django-axes 登录 Django

javascript - 在 Mongo 中查找包含 ISODate 字段的文档

mongodb - 具有分页数据和总计的 Mongo 聚合

python - Scrapy 爬行仅第一页

mysql - 具有 3000 万条目的表速度很慢。优化MySQL还是改用mongodb?

java - Spring 数据 - 从分组中计算不同的项目

java - 使用吗啡数据存储在 mongodb 中查询和更新

mongodb - 如何检查是否存在多个文档