vedant sali
vedant sali

Reputation: 80

couchDB- complex query on a view

I am using cloudantDB and want to query a view which looks like this

function (doc) { if(doc.name !== undefined){ emit([doc.name, doc.age], doc); }

what should be the correct way to get a result if I have a list of names(I will be using option 'keys=[]' for it) and a range of age(for which startkey and endkey should be used)

example: I want to get persons having name "john" or "mark" or "joseph" or "santosh" and lie between age limit 20 to 30.

If i go for list of names, query should be keys=["john", ....] and if I go for age query should use startkey and endkey

I want to do both :)

Thanks

Upvotes: 0

Views: 458

Answers (1)

Alexis Côté
Alexis Côté

Reputation: 3690

Unfortunately, you can't do so. Using the keys parameter query the documents with the specified key. For example, you can't only send keys=["John","Mark"]&startkey=[null,20]&endkey=[{},30]. This query would only and ONLY return the document having the name John and Mark with a null age.

In your question you specified CouchDB but if you are using Cloudant, index query might be interesting for you.

You could have something like that :

{
  "selector": {
    "$and": [
      {
        "name": {
           "$in":["Mark","John"]
         }
      },
      {
        "year": {
          "$gt": 20,
          "$lt": 30
        }
      }
    ]
  },
  "fields": [
    "name",
    "age"
  ]
}

As for CouchDB, you need to either separate your request (1 request for the age and 1 for the people) or you do the filtering locally.

Upvotes: 1

Related Questions