How to search an array

I have documents with the following format:

{
 url: 'some-unique-url',
 name: 'some-name'
}

      

What I need to do is select documents with a specific URL by providing an array containing the URL I need to select:

['some-unique-url', 'another-url']

      

This is what my view looks like:

function(doc) {
  if(doc.type == 'message'){
    emit([doc.url], null);
  }
}

      

And here is my node.js code. I am using nano .

db.view('message', 'by_url', {'key': urls}, function(err, body){
    res.send(body);
});

      

This works if I only have one element in the array, but as soon as I add another element, this is what I get:

{"total_rows":18,"offset":11,"rows":[]}

      

I have also tried startkey

and endkey

, which also works, but it works the same as the previous one:

db.view('message', 'by_url', {'startkey': online_users_ids, 'endkey': [online_users_ids, {}]}, function(err, body){
    res.send(body);
});

      

Am I trying to do this with couchdb and nano? If not, what is the closest thing I can get without losing performance? Thanks in advance!

+3


source to share


1 answer


You need to use keys

instead key

as described in the CouchDB API Reference .



db.view('message', 'by_url', {'keys': urls}, function(err, body){
    res.send(body);
});

      

+4


source







All Articles