2017-03-02 39 views
1

我正在构建Web刮板并尝试为实体分配UUID。在MongoDB中分配UUID并检查网页扫描器的重复内容

因为一个实体可以在不同的时间刮,我想最初的UUID存储与从网页

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

提取的ID一起的是,对于在找到的每一个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查找命令返回你需要遍历或访问来获取对象

访问第一个结果光标对象(你已经选中一个存在),并访问ent_uid领域。

大概你会搜索EID类型,ent_eid不是ent_uid。没有理由搜索你是否已经拥有它。

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']