[英]Retrieve the fields that have been updated in a record
Can I retrieve the fields that have been updated in a view? 我可以检索视图中已更新的字段吗? Ideally I would be able to retrieve the total number of fields that have been updated (item added to list field or field changed) and the names/count of the individual fields.
理想情况下,我将能够检索已更新的字段总数(添加到列表字段或更改的字段的项目)以及各个字段的名称/数量。
Given a db record such as: 给定一个数据库记录,例如:
{
"_id": "e3cfb5e19e5c05fb6822c5ee228c4f2e",
"_rev": "52-c78a26776b0e0736dd45e44a47634031",
"last_updated": "23/04/2015",
"title", "testdoc",
"samples": [
{
"confidence": "high",
"handle": "joetest"
}
],
"locations": [
{"Toronto", "Ontario"},
{"Vancouver", "British Columbia"}
]
}
If there are 2 new items in "samples" and 1 in "locations" I would see an updated count of 3. 如果“样本”中有2个新项目,而“位置”中有1个新项目,则更新后的计数为3。
Assuming you retrieve the data before it's updated, you can compare it to the data after it's updated. 假设您在更新之前检索数据,则可以将其与更新之后的数据进行比较。 The following simple function looks at any keys with arrays in the original data and returns the length of the new data.
以下简单函数查看原始数据中具有数组的任何键,并返回新数据的长度。
def list_delta(before, after):
out = {}
for key in before:
if isinstance(before[key], list):
out[key] = len(after[key]) - len(before[key])
return out
before = {'name': 'davidism', 'numbers': [0, 1, 2, 3, 4]}
after = do_update(before)
# after = {'name': 'davidism', 'numbers': [0, 1, 2, 3, 4, 5]}
list_delta(before, after)
# returns {'numbers': 1}
You can get more clever and compare if keys were added or removed, or if the actual values changed, or do a deeper recursive comparison for nested structures, but that's beyond the scope of this answer. 您可以更聪明地进行比较,以比较是否添加或删除了键,或者实际值是否发生了变化,或者对嵌套结构进行了更深入的递归比较,但这超出了此答案的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.