Search code examples
couchdbcloudant

How to make multiple IN ["V1", "V3","V5"] query


For a documents of that has the following structure

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

where the countryCode has limited list of options (ISO country codes) and the status has a limited set of options too I need to select only the documents that are for the given list of countries basically and given list of statuses

in SQL means it would be something like

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

is it at all possible in Cloudant / CouchDB?


Solution

  • With CouchDB's /db/_find, the following selector produces the desired result:

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

    Condition operators such as $in are specific to a field, and are used to evaluate the value stored in that field.

    CURL

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