Python/Dictionary/List/Mongo插入问题开始

2024-09-30 07:35:51 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,正在尝试理解并习惯使用字典和列表对象。在

我正在通过ebaysdk调用eBay的API,并希望将其中的商品作为文档存储在Mongo中。简单。在

以下是将返回的架构示例:

<timestamp>2009-09-04T00:47:12.456Z</timestamp>
  <searchResult count="2">
  <item>
     <itemId>230371938681</itemId>
     <title>Harry Potter and the Order of the Phoenix HD-DVD</title>
     <globalId>EBAY-US</globalId>
     <primaryCategory>
        <categoryId>617</categoryId>
        <categoryName>DVD, HD DVD & Blu-ray</categoryName>
     </primaryCategory>

我已经尝试了500次代码迭代,这里是我所拥有的最基本的代码。在

^{pr2}$

将得到以下错误:

Traceback (most recent call last):
  File "ebay_search.py", line 34, in <module>
    ebay_collection.insert(key)
  File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 408, in insert
    self.uuid_subtype, client)
  File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 378, in gen
    doc['_id'] = ObjectId()
TypeError: 'str' object does not support item assignment

简单的东西。我要做的就是把每一项都作为一个文档添加。在


Tags: thein文档pytitlelineitemtimestamp
1条回答
网友
1楼 · 发布于 2024-09-30 07:35:51

像字符串这样的不可变类型不能用作文档,因为它不允许添加其他字段,如^{} field Mongo requires。您可以改为将字符串包装在字典中作为包装文档:

key_doc = {'key': key}
ebay_collection.insert(key_doc)

相关问题 更多 >

    热门问题