gpt4 book ai didi

python - Pymongo:bson.errors.InvalidDocument:无法编码对象:

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:32 29 4
gpt4 key购买 nike

我正在尝试使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com