How do I query X specific documents all at once using an index with pouchdb?

328 views Asked by At

Let's say I have an array of X ids. There is no sequence to them - it is just a list of X random ids.

Now, I want to query X documents using this array, and return it as a SINGLE result - like an array of documents, not make 5 separate queries.

I know I can create a map() function on the fly to do this - essentially looking for documents with an id in the array of ids... but then the query will always be slow. What I'd love to learn how to do is make it fast, like using a secondary index.

Is there a way to do this with PouchDB? And if there isn't a way to do this, how can I easily chain together X promises returned from X queries and group them all in a single result - probably the next best thing?

2

There are 2 answers

0
nlawson On

To search for an array of _ids, you can use allDocs() and keys:

db.allDocs({include_docs: true, keys: ['a', 'b', 'c']})
  .then(/* ... */)
  .catch(/* ... */);

There's no need for a secondary index, unless you want to search for something other than _id. In which case you might look into queries or pouchdb-find.

0
Angel Paraskov On

You can give a try to Datalog indexes for PouchDB and do complex queries.

Please check the plugin here