在 Apache CouchDB 中按 JSON 对象的参数值过滤视图

Filter view by JSON object's parameter value in Apache CouchDB

对于 Apache CouchDB 实例,定义了一个现有视图 - viewPrincipals。我通过 URI(X_db/_design/XDoc/_view/viewAllPrincipals) 得到的响应是:

{
   "total_rows":2,
   "offset":0,
   "rows":[
      {
         "id":"##SP:hosttest1.ite1.com@ITE1.IDNG.COM",
         "key":"##SP:hosttest1.ite1.com@ITE1.IDNG.COM",
         "value":"2-80da25dcdcb8bae15019929bfd577893"
      },
      {
         "id":"##SP:hosttest200.ite1100.com@ITE1100.IDNG.COM",
         "key":"##SP:hosttest200.ite1100.com@ITE1100.IDNG.COM",
         "value":"2-80da25dcdcb8bae15019929bfd577893"
      }
   ]
}

现在我需要通过 URI 查询过滤此 JSON 响应,以便我只获得具有 id starting 的 JSON 对象 与“##SP:hosttest1.ite1.com” 是否可以通过 URI 参数在 CouchDB 中实现过滤?

您可以使用这样的查询:

GET /X_db/_design/XDoc/_view/viewAllPrincipals??startkey="##SP:hosttest1.ite1.com"&endkey="##SP:hosttest1.ite1.com\uffff"

你可以看看relevant section of the documentation


解释:

我有一个名为 title 的视图,它是 indexing/sorting 我的文档,根据 key(在我的视图映射函数中定义),如下所示:

$ curl -k -X GET https://admin:****@192.168.1.106:6984/sample/_design/title/_view/title
{"total_rows":19,"offset":0,"rows":[
{"id":"doc8","key":"Adam","value":["אדאם יהוה"]},
{"id":"doc14","key":"Average Joe","value":[null]},
{"id":"doc2","key":"between","value":["בין"]},
{"id":"doc12","key":"Cool","value":[null]},
{"id":"doc7","key":"David Salomon","value":["דיויד שלמן"]},
{"id":"doc1","key":"Earth","value":["ארץ"]},
{"id":"doc13","key":"Geek","value":[null]},
{"id":"doc0","key":"Hello elephant","value":["שלום פיל"]},
{"id":"doc5","key":"Jacob","value":["יעקב"]},
{"id":"doc6","key":"Jacob","value":["ישראל"]},
{"id":"doc4","key":"on you","value":["עליכם"]},
{"id":"doc11","key":"Pretty","value":[null]},
{"id":"doc10","key":"Sir","value":[null]},
{"id":"doc9","key":"Start","value":["ראש"]},
{"id":"doc3","key":"telmeg","value":["מגידו"]},
...
]}

现在,如果我想查询 title 视图以仅获取以字母 S 开头的文档,我可以添加如下查询参数:

?startkey="S"&endkey="S\uffff"

由于 S 是第一个可能的情况,而 \uffff 是最后一个可能的 Unicode,它涵盖了 wild-card 表示法中等同于 S* 的所有情况。因此,我可以通过 运行:

获取所有以 S 开头的文档
$ curl -k -X GET 'https://admin:****@192.168.1.106:6984/sample/_design/title/_view/title?startkey="S"&endkey="S\uffff"'
{"total_rows":19,"offset":12,"rows":[
{"id":"doc10","key":"Sir","value":[null]},
{"id":"doc9","key":"Start","value":["ראש"]}
]}

The documentation 使用的是 \ufff0,但我使用的是 \uffff,并且有效。我不太确定其中的区别。也许我错过了什么。