0

我有以下看法:

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 来实现我的目标。

4

2 回答 2

0

我不知道 Couchbase,但是写这样的东西怎么样:

 emit(parseInt(doc.versionNumber.value),doc.setId.extension)

然后查询它:

?descending=true&&limit=1

您还可以使用 map reduce:如何在 CouchDB 中使用 Map-Reduce 计算最大值?,或者考虑使用 N1QL,实现 MIN 和 MAX SQL 查询

于 2015-06-26T13:25:10.333 回答
0

好吧,感谢@Leogiciel的 Max reduce 功能,我终于找到了解决方案。首先,视图定义保持不变。解决方案是使用Max 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}
]
}
于 2015-07-01T15:50:43.020 回答