背景 - 我正在 CouchDb 中存储一些推文,我想通过使用 Twitter Id 作为 CouchDb 文档 ID 来轻松防止存储重复的推文。
我正在使用 Python 脚本使用 python-twitter 库从 Twitter 中提取一些推文,该库返回一个对象集合,每个对象都包含唯一的 Twitter 推文 ID 作为属性 (twitter.Status.id)。在将推文保存到 CouchDb 时,我想将其用作 CouchDb 文档 ID。
>>> import twitter
>>> api = twitter.Api()
>>> statuses = api.GetSearch('xyz')
>>> s = statuses[0] # save just the first one for now
>>> import couchdb
>>> couch = couchdb.Server()
>>> dbcouch = couch['tweets']
>>> dbcouch.save(s.AsDict())
(u'fd55e5944267266892f076891a3d9ac4', u'1-4a50a618afd4dc68373155b1ad3e96a1')
CouchDb 设置了唯一的Id,这不是我想要的。该文档有从头开始构建文档时设置手动文档 ID 的示例,但在这种情况下(对象交给我的地方)我似乎无法使其工作。
请您参考如下方法:
在将状态保存到沙发之前,您需要在状态中获取一个 "_id" 字段。例如(我的 Python 生锈了)
>>> s = statuses[0] # save just the first one for now
>>> tweet = s.AsDict()
>>> tweet["_id"] = "%d" % tweet["id"]
>>> # (Couch stuff same as before)
>>> dbcouch.save(tweet)
它是如何工作的?
