2017-03-02 5 views
1

私はWebスクレーパーを構築しており、エンティティーにUUIDを割り当てようとしています。mongodbのUUIDの割り当てとWebスクレイパーの重複のチェック

一方のエンティティは、異なる時間に掻きすることができるので、私は以下のWebページ

// example document 
{ 
"ent_eid_type": "ABC-123", 
"ent_uid_type": "123e4567-aaa-123e456" 
} 

から抽出されたIDと共に初期のUUIDを格納するには、Aに見出されるすべてのIDフィールドのために実行されるコードであります削られた品目

# if the current ent_eid_type is a key in mongo... 
if db_coll.find({ent_eid_type: ent_eid}).count() > 0: 

    # return the uid value 
    ent_uid = db_coll.find({ent_uid_type: ent_uid }) 
else: 
    # create a fresh uid 
    ent_uid = uuid.uuid4() 

    # store it with the current entity eid as key, and uid as value 
    db_coll.insert({ent_eid_type: ent_eid, ent_uid_type: ent_uid}) 

# update the current item with the stored uid for later use 
item[ent_uid_type] = ent_uid 

コンソールが返品KeyError: <pymongo.cursor.Cursor object at 0x104d41710>です。 ent_uidのカーソルを解析する方法がわからない

ヒント/ご提案ありがとうございます!

答えて

1

Pymongoコマンドは、あなたが(あなたがすでに1が存在してチェックする)オブジェクトに最初の結果を

アクセスを得るために反復またはアクセスする必要がある、とent_uidフィールドにアクセスカーソルオブジェクトを返して下さい。

おそらく、あなたはent_eidではなくent_eidでEIDタイプを検索します。あなたがすでにそれを持っている場合、検索する理由はありません。

ent_uid = db_coll.find({ent_eid_type: ent_eid })[0]['ent_uid'] 

またはカーソルを心配し、代わりにfind_oneコマンドを使用していない(http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one

ent_uid = db_coll.find_one({ent_eid_type: ent_eid })['ent_uid'] 
関連する問題