I would like to create a view with map / reduce functions that return the latest values from several documents with same id, seems I can return the latest document giving with the xyz id based on the update_datetime, not sure how to get the latest value like field1 and field2, thank you
我想用map / reduce函数创建一个视图,它返回来自几个具有相同id的文档的最新值,似乎我可以返回基于update_datetime的xyz id给出的最新文档,不知道如何获取最新值如field1和field2,谢谢
doc1: id='xyz', field1='v1.0', field2='v2.0', field3='v3.0', status='NEW', update_datetime='2015-04-28'
doc2: id='xyz', field2='v2.1', field3='v3.0', status='CHANGED', update_datetime='2015-05-01'
doc3: id='xyz', field3='v3.1', status='CLOSED', update_datetime='2015-05-10'
define a view with map / reduce that, given a key value 'xyz', returns the following document:
使用map / reduce定义一个视图,给定一个键值'xyz',返回以下文档:
id='xyz', field1='v1.0', field2='v2.1', field3='v3.1', status='CLOSED', update_datetime='2015-05-10'.
1 个解决方案
#1
0
I have a feeling that the data model you have could be improved but I do not know enough about your use case to comment on that really. Therefore I would suggest a reduce function that will perform a merge of the documents using newer as an override for existing properties unless a property does not exist. Here is the content of map and reduce functions (btw, I have changed the name of one of the fields from update_datetime
to updated
. Also please bear in mind that id
and _id
are different - might be worth renaming your own id field to something more distinguishable from couchdb's doc id field):
我有一种感觉,你可以改进你的数据模型,但我不太了解你的用例,以便对此进行评论。因此,我建议使用reduce函数来执行文档的合并,使用newer作为现有属性的覆盖,除非属性不存在。这是map和reduce函数的内容(顺便说一下,我已将其中一个字段的名称从update_datetime更改为更新。另请注意id和_id是不同的 - 可能值得将自己的id字段重命名为更多内容可以与couchdb的doc id字段区分开来):
// MAP
function(doc) {
emit(doc.id, doc);
}
// REDUCE
function(keys, values, rereduce) {
if (values.length > 1) {
var mergedDoc = values[0];
for (var i = 1; i < values.length; i++) {
var newDoc = values[i];
var updatingWithNewer = newDoc.updated >= mergedDoc.updated;
for (key in newDoc) {
if (key != "_id" && key != "id") {
if (updatingWithNewer || mergedDoc[key] == undefined) {
mergedDoc[key] = newDoc[key];
}
}
}
}
return mergedDoc;
} else {
return values[0];
}
}
Performing level 1 reduce will give you expected result. Hope it helps.
执行1级减少将为您提供预期结果。希望能帮助到你。
#1
0
I have a feeling that the data model you have could be improved but I do not know enough about your use case to comment on that really. Therefore I would suggest a reduce function that will perform a merge of the documents using newer as an override for existing properties unless a property does not exist. Here is the content of map and reduce functions (btw, I have changed the name of one of the fields from update_datetime
to updated
. Also please bear in mind that id
and _id
are different - might be worth renaming your own id field to something more distinguishable from couchdb's doc id field):
我有一种感觉,你可以改进你的数据模型,但我不太了解你的用例,以便对此进行评论。因此,我建议使用reduce函数来执行文档的合并,使用newer作为现有属性的覆盖,除非属性不存在。这是map和reduce函数的内容(顺便说一下,我已将其中一个字段的名称从update_datetime更改为更新。另请注意id和_id是不同的 - 可能值得将自己的id字段重命名为更多内容可以与couchdb的doc id字段区分开来):
// MAP
function(doc) {
emit(doc.id, doc);
}
// REDUCE
function(keys, values, rereduce) {
if (values.length > 1) {
var mergedDoc = values[0];
for (var i = 1; i < values.length; i++) {
var newDoc = values[i];
var updatingWithNewer = newDoc.updated >= mergedDoc.updated;
for (key in newDoc) {
if (key != "_id" && key != "id") {
if (updatingWithNewer || mergedDoc[key] == undefined) {
mergedDoc[key] = newDoc[key];
}
}
}
}
return mergedDoc;
} else {
return values[0];
}
}
Performing level 1 reduce will give you expected result. Hope it helps.
执行1级减少将为您提供预期结果。希望能帮助到你。