如何将多个CouchDB查询组合到一个请求中?

我正在尝试查询Cloudant.com数据库(CouchDB)中的文档.以下两个查询请求分别正常工作:

{ "selector": { "some_field": "value_1" } }

{ "selector": { "some_field": "value_2" } }

Cloudant的文档似乎表明我应该能够将这两个查询组合成一个HTTP请求,如下所示:

{ "selector": { "$or": [ { "some_field": "value_1" }, 
                         { "some_field": "value_2" } ] } }

但是当我尝试时,我收到以下回复:

{"error":"no_usable_index",
 "reason":"There is no operator in this selector can used with an index."}

有人能告诉我我需要做些什么才能让它发挥作用?

最佳答案 目前似乎还没有办法通过Cloudant Query实现这一目标.但是,您可以使用通过Cloudant Query创建的索引来使用视图查询.假设索引位于名为ae97413b0892b3738572e05b2101cdd303701bb8的设计文档中:

curl -X POST \
'https://youraccount.cloudant.com/db/_design/ae97413b0892b3738572e05b2101cdd303701bb8/_view/ae97413b0892b3738572e05b2101cdd303701bb8?reduce=false&include_docs=true' \
-d '
{
  "keys":[
    ["value_1"],
    ["value_2"]
  ]
}'

这会给你一个像这样的回复:

{
  "total_rows": 3,
  "offset": 1,
  "rows": [
    {
      "id": "5fcec42ba5cad4fb48a676400dc8f127",
      "key": [
        "abc"
      ],
      "value": null,
      "doc": {
        "_id": "5fcec42ba5cad4fb48a676400dc8f127",
        "_rev": "1-0042bf88a7d830e9fdb0326ae957e3bc",
        "some_field": "value_1"
      }
    },
    {
      "id": "955606432c9d3aaa48cab0c34dc2a9c8",
      "key": [
        "ghi"
      ],
      "value": null,
      "doc": {
        "_id": "955606432c9d3aaa48cab0c34dc2a9c8",
        "_rev": "1-68fac0c180923a2bf133132301b1c15e",
        "some_field": "value_2"
      }
    }
  ]
}
点赞