Python - Cloudant 获取更改
Python - Cloudant Get Changes
我正在使用 Cloudant 库从 Cloudant 数据库收集文档。每次我 运行 python 脚本时,我都会得到所有文档,但我只想检索上次执行脚本时添加的文档,换句话说,get_changes 函数。
我已经搜索过答案,但似乎并不容易找到。
谢谢大家的帮助,
菲利波。
使用changes()
方法。跟踪最后一个序列 ID,并从那里重新启动以仅检索看不见的更改。
# Iterate over a "normal" _changes feed
changes = db.changes()
for change in changes:
print(change)
# ...time passes
new_changes = db.changes(since=changes.last_seq)
for new_change in new_changes:
print(new_change)
如果你也想要doc正文,可以传include_docs=True
.
见https://github.com/cloudant/python-cloudant/blob/master/src/cloudant/database.py#L458
如果您只想捕获新增内容(而不是所有更改),您可以按照以下行在数据库设计文档中创建过滤器函数:
function(doc, req) {
// Skip deleted docs
if (doc._deleted) {
return false;
}
// Skip design docs
if (doc._id.startsWith('_design')) {
return false;
}
// Skip updates
if (!doc._rev.startsWith('1-')) {
return false;
}
return true;
}
并将其应用于更改提要:
new_changes = db.changes(since=changes.last_seq, filter='myddoc/myfilter'):
# do stuff here
但在 Python 代码中简单地获取所有更改和过滤可能同样容易。
我正在使用 Cloudant 库从 Cloudant 数据库收集文档。每次我 运行 python 脚本时,我都会得到所有文档,但我只想检索上次执行脚本时添加的文档,换句话说,get_changes 函数。
我已经搜索过答案,但似乎并不容易找到。
谢谢大家的帮助,
菲利波。
使用changes()
方法。跟踪最后一个序列 ID,并从那里重新启动以仅检索看不见的更改。
# Iterate over a "normal" _changes feed
changes = db.changes()
for change in changes:
print(change)
# ...time passes
new_changes = db.changes(since=changes.last_seq)
for new_change in new_changes:
print(new_change)
如果你也想要doc正文,可以传include_docs=True
.
见https://github.com/cloudant/python-cloudant/blob/master/src/cloudant/database.py#L458
如果您只想捕获新增内容(而不是所有更改),您可以按照以下行在数据库设计文档中创建过滤器函数:
function(doc, req) {
// Skip deleted docs
if (doc._deleted) {
return false;
}
// Skip design docs
if (doc._id.startsWith('_design')) {
return false;
}
// Skip updates
if (!doc._rev.startsWith('1-')) {
return false;
}
return true;
}
并将其应用于更改提要:
new_changes = db.changes(since=changes.last_seq, filter='myddoc/myfilter'):
# do stuff here
但在 Python 代码中简单地获取所有更改和过滤可能同样容易。