簡體   English   中英

Python / Dictionary / List / Mongo插入問題 - 初學者

[英]Python / Dictionary / List / Mongo insert issues - beginner

對不起,試着理解並習慣了字典和列表對象。

我通過他們的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次這段代碼的迭代,這是我最基本的東西。

from ebaysdk import finding
from pymongo import MongoClient

api = finding(appid="billy-40d0a7e49d87")
api.execute('findItemsByKeywords', {'keywords': 'potter'})
listings = api.response_dict()

client = MongoClient('mongodb://user:pass@billy.mongohq.com:10099/ebaystuff')
db = client['ebaycollection']
ebay_collection = db.ebaysearch

for key in listings:
  print key
  ebay_collection.insert(key)

會得到這個錯誤:

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

簡單的東西。 我想要做的就是將每個項目添加為文檔。

像字符串這樣的不可變類型不能用作文檔,因為它不允許添加其他字段,例如Mongo要求_id字段 您可以將字符串包裝在字典中以用作包裝文檔:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM