Roy
Roy

Reputation: 910

How to pass array of inputs to node Js query selector - cloudant

I'm trying to fetch documents from cloudant db with node js. Here for one single input value I'm able to get results, but with array of input's I'm confusing to write query.

This is my node js query part for single input:

var query = {
   "selector": 
   {
      "name": 'name1',
   },
   "fields": [
      "_id",
      "_rev",
      "subjects",
      "name" 
   ],
};
For array of names:

var query = {
   "selector": 
   {
      "name": {$in: ['name1', 'name2']},
   }
};

Here for name field input, i want to pass multiple names in an array. So that what ever names match those documents should return and the above query for multiple names is not working. Any help or suggestion will be appreciated.

Upvotes: 1

Views: 149

Answers (2)

ricellis
ricellis

Reputation: 396

If you want to match documents with either name1 or name2, then use the $or combination operator e.g.

var query = {
   "selector": 
   {
      "$or": [
          { "name": "name1" },
          { "name": "name2" }
      ]
   }
};

Upvotes: 1

vinothM
vinothM

Reputation: 195

I dont think there is $in operator but there is $all which will match array of values

Try

'$all': ['name1', 'name2']

Upvotes: 0

Related Questions