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

时间:2014-12-06 01:07:00

标签: couchdb cloudant

我试图在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."}

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

1 个答案:

答案 0 :(得分:2)

目前还没有一种方法可以通过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"
      }
    }
  ]
}