如何进行多个IN [“ V1”,“ V3”,“ V5”]查询

问题描述

对于具有以下结构的文档

{
  "countryCode": "US","status" : "Pending"
}

其中countryCode的选项列表有限(ISO国家/地区代码) 而且status的选项集也很有限,我只需要选择 基本用于给定的国家列表和状态列表

sql

表示类似

countryCode IN ["US","AR","UK"] AND status IN ["Pending","Error","Loading"]

在Cloudant / CouchDB中有可能吗?

解决方法

使用CouchDB的/db/_find,以下selector会产生所需的结果:

{
   "selector":{
      "$and":[
         {
            "countryCode":{
               "$in":["US","AR","UK"]
            }
         },{
            "status":{
               "$in":["Pending","Error","Loading"]
            }
         }
      ]
   }
}

Condition operators(例如$in)特定于某个字段,用于评估存储在该字段中的值。

CURL

curl -H 'Content-Type: application/json' -X POST http://localhost:5984/<db>/_find -d '{"selector":{"$and":[{"countryCode":{"$in":["US","UK"]}},{"status":{"$in":["Pending","Loading"]}}]}}'