Python 如何解决TypeError:规范必须是pymongo上dict的实例
我有一个mongodb集合,我必须根据文档中是否存在子文档将一些数据插入其中。集合中的示例文档是Python 如何解决TypeError:规范必须是pymongo上dict的实例,python,mongodb,dictionary,pymongo,Python,Mongodb,Dictionary,Pymongo,我有一个mongodb集合,我必须根据文档中是否存在子文档将一些数据插入其中。集合中的示例文档是 {"follow_request_sent": "null", "profile_use_background_image": "true", "default_profile_image": "false", "id": 87174680, "verified": "false", "profile_imag
{"follow_request_sent": "null",
"profile_use_background_image": "true",
"default_profile_image": "false",
"id": 87174680,
"verified": "false",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/378800000220301249/a0c7b8c5766de83b65a42ca52196c4b3_normal.jpeg",
"profile_sidebar_fill_color": "EADEAA",
"profile_text_color": "333333",
"followers_count": 348,
"profile_sidebar_border_color": "D9B17E",
"id_str": "87174680",
"profile_background_color": "8B542B",
"listed_count": 5,
"profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/350203578/Photo0003.jpg",
"utc_offset": 19800,
"statuses_count": 20119,
"description": "Sports Lover",
"friends_count": 708,
"location": "India",
"profile_link_color": "9D582E",
"profile_image_url": "http://pbs.twimg.com/profile_images/378800000220301249/a0c7b8c5766de83b65a42ca52196c4b3_normal.jpeg",
"following": "null",
"geo_enabled": "true",
"profile_background_image_url": "http://a0.twimg.com/profile_background_images/350203578/Photo0003.jpg",
"name": "Ronak Baj",
"lang": "en",
"profile_background_tile": "true",
"favourites_count": 17,
"screen_name": "ronakbaj",
"notifications": "null",
"url": "null",
"created_at": "Tue Nov 03 12:02:56 +0000 2009",
"contributors_enabled": "false",
"time_zone": "New Delhi",
"protected": "false",
"default_profile": "false",
"is_translator": "false"
},
我必须根据id的值更新它,这样,如果id已经在文档中,就更新它,否则就创建新文档。我使用的语法是:
posts4 = db4.posts
post_id4 = posts4.update(posts4.find_one({'id' : usr.get('id')}), dict4, upsert = True)
File "new.py", line 63, in <module>
stream.statuses.filter(follow = 95995660)
File "/usr/lib/python2.7/site-packages/twython/streaming/types.py", line 65, in filter
self.streamer._request(url, 'POST', params=params)
File "/usr/lib/python2.7/site-packages/twython/streaming/api.py", line 148, in _request
if self.on_success(data): # pragma: no cover
File "new.py", line 48, in on_success
post_id4 = posts4.update(posts4.find_one({'id' : usr.get('id')}), dict4, upsert = True)
File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 463, in update
raise TypeError("spec must be an instance of dict")
TypeError: spec must be an instance of dict
dict4
是要更新的新文档,如果找到id
但是,这给了我一个错误,对此的完整回溯是:
posts4 = db4.posts
post_id4 = posts4.update(posts4.find_one({'id' : usr.get('id')}), dict4, upsert = True)
File "new.py", line 63, in <module>
stream.statuses.filter(follow = 95995660)
File "/usr/lib/python2.7/site-packages/twython/streaming/types.py", line 65, in filter
self.streamer._request(url, 'POST', params=params)
File "/usr/lib/python2.7/site-packages/twython/streaming/api.py", line 148, in _request
if self.on_success(data): # pragma: no cover
File "new.py", line 48, in on_success
post_id4 = posts4.update(posts4.find_one({'id' : usr.get('id')}), dict4, upsert = True)
File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 463, in update
raise TypeError("spec must be an instance of dict")
TypeError: spec must be an instance of dict
文件“new.py”,第63行,在
stream.status.filter(follow=95995660)
文件“/usr/lib/python2.7/site packages/twython/streaming/types.py”,第65行,在过滤器中
self.streamer._请求(url,'POST',params=params)
文件“/usr/lib/python2.7/site packages/twython/streaming/api.py”,第148行,在请求中
如果自我成功(数据):#布拉格语:无封面
文件“new.py”,第48行,on_success
post_id4=posts4.update(posts4.find_one({'id':usr.get('id')),dict4,upsert=True)
文件“/usr/lib64/python2.7/site packages/pymongo/collection.py”,第463行,在更新中
raise TypeError(“规范必须是dict的实例”)
TypeError:规范必须是dict的实例
请帮忙 当作为更新参数传递时,要更新的搜索条件必须是字典。 您可以直接传递字典进行更新,如下所示:
posts4 = db4.posts
post_id4 = posts4.update({'id' : usr.get('id')}, dict4, upsert = True)
您可以查看详细信息I bet
posts4.find_one({'id':usr.get('id'))
返回None
。检查它打印的内容:打印类型(posts4.find_one({'id':usr.get('id')))
@alecxe,您是对的,它不返回任何内容。但这不是upsert的全部要点吗?如果没有找到与规范匹配的实例,则更新update
要求第一个参数是dict
类型(请参阅)。您不需要显式调用find_one
。试试:posts4.update({'id':usr.get('id')},dict4,upsert=True)
@alecxe谢谢,效果很好