我有以下观点:
function (doc, meta) {
if (meta.type == "json")
if ( doc.id
&& doc.id.root.indexOf("1.2.250.1.199.16.1.1") == 0
&& doc.setId
&& doc.versionNumber)
{
emit(doc.setId.extension, parseInt(doc.versionNumber.value))
}
}
返回以下结果集:
Key Value
"4816f76e-33f9-456a-9e23-961799883994" 1
wound2QualificationRev1
"4816f76e-33f9-456a-9e23-961799883994" 2
wound2QualificationRev2
现在我想将最大值的结果集限制为仅返回
Key Value
"4816f76e-33f9-456a-9e23-961799883994" 2
wound2QualificationRev2
我如何修改视图?使用自定义reduce方法?
编辑1 :请注意,此处显示的结果集是部分的。我无法使用orderby和limit来实现我的目标。
答案 0 :(得分:0)
我不知道Couchbase,但是写一些像:
emit(parseInt(doc.versionNumber.value),doc.setId.extension)
然后用:
查询它?descending=true&&limit=1
你也可以使用map reduce:How to calculate Max value using Map-Reduce in CouchDB?,或者考虑使用N1QL,它实现了MIN和MAX SQL查询
答案 1 :(得分:0)
Kudos 到@Leogiciel的Max reduce功能,我终于找到了解决方案。 首先,视图定义保持不变。 解决方案是使用Max reduce function的这种实现,最重要的是使用查询参数 group和reduce 。
初始结果集,此处显示为原始Json:
{"total_rows":9,"rows":[
{"id":"wound1Image1","key":"0a41d087-1a7f-4d9f-8df9-83d0deb2566c","value":1},
{"id":"wound2QualificationRev1","key":"4816f76e-33f9-456a-9e23-961799883994","value":1},
{"id":"wound2QualificationRev2","key":"4816f76e-33f9-456a-9e23-961799883994","value":2},
{"id":"wound2Image3","key":"65ff979a-e1e0-43d4-a24f-a3ee0cc8b67c","value":1},
{"id":"wound2Image1","key":"69f29cf8-3ee7-4681-937d-d465edc9bb16","value":1},
{"id":"wound1Image2","key":"76ab9a67-a17b-4fc6-a105-60b57e971d4f","value":1},
{"id":"wound1Synthesis","key":"bc1a7ec0-30d7-4493-8dd9-dcf6d0b48cc9","value":1},
{"id":"wound1Qualification","key":"cb375a5f-e07d-449a-ab13-60dba16a0323","value":1},
{"id":"wound2Image2","key":"dfb5e7eb-6192-46ba-84d2-0e5996536930","value":1}
]
}
使用以下查询参数:
?group=true&reduce=true
返回正确的结果集:
{"rows":[
{"key":"0a41d087-1a7f-4d9f-8df9-83d0deb2566c","value":1},
{"key":"4816f76e-33f9-456a-9e23-961799883994","value":2},
{"key":"65ff979a-e1e0-43d4-a24f-a3ee0cc8b67c","value":1},
{"key":"69f29cf8-3ee7-4681-937d-d465edc9bb16","value":1},
{"key":"76ab9a67-a17b-4fc6-a105-60b57e971d4f","value":1},
{"key":"bc1a7ec0-30d7-4493-8dd9-dcf6d0b48cc9","value":1},
{"key":"cb375a5f-e07d-449a-ab13-60dba16a0323","value":1}, {"key":"dfb5e7eb-6192-46ba-84d2-0e5996536930","value":1}
]
}