PouchDB view lookup by key

45 views
Skip to first unread message

Stas Altgovzen

unread,
Mar 14, 2017, 12:36:09 PM3/14/17
to PouchDB

I have an CouchDB database named t-customers. Using Fauxton I've created the following view t-customers/_design/t-cust-design/_view/by-custdes. Here is the map function:

function (doc) {
  var custname = doc.CUSTNAME;
  if(custname != undefined && custname.length != undefined && custname.length != ''){
    for(var i = 0; i < custname.length - 1; i++)
      for(var j = i + 1; j < custname.length + 1; j++)
          emit(custname.substring(i, j),doc._id);
  }
}

The view will contain all available sub-strings for custdes (e.g. custdes=abc -> a, ab, abc, bc) as key and doc._id as its value.

After the view is created I can query it with the following http requests:

  • http://127:0.0.1:5984/t-customers/_design/t-cust-design/_view/by-custdes?key="ab"
  • http://127:0.0.1:5984/t-customers/_design/t-cust-design/_view/by-custdes?key="abc"

It works as fast as lightning although my view has about 1.500.000 indexes.

First of all: I've noticed that the PochdbBD syncs only the t-customers database and not it's view. Why? To make the view avaliable in PouchDB it requires for me to run the following command which takes up to 20 minutes to complete:

t-customers.query("t-cust-design/by-custdes").then(...).catch(...);

Only and only then I can see the view at IndexedDB in Chrome.

Second of all: What is the way to lookup for doc in PouchDB view t-cust-design/by-custdes without triggering the whole map/reduce process every time I want to find the ab key? As I mentioned I can query the CouchDB _design/t-cust-design/_view/by-custdes view with http request and it works fast, but I'm unable to do the equivalent action using PouchDB API.

I've read tons of documentation but I'm still confused about it...

Giovanni P

unread,
Mar 15, 2017, 5:06:53 AM3/15/17
to pou...@googlegroups.com

On 14 Mar 2017 13:36, "Stas Altgovzen" <altg...@gmail.com> wrote:
>
> I have an CouchDB database named t-customers. Using Fauxton I've created the following view t-customers/_design/t-cust-design/_view/by-custdes. Here is the map function:
>
> function (doc) { var custname = doc.CUSTNAME; if(custname != undefined && custname.length != undefined && custname.length != ''){ for(var i = 0; i < custname.length - 1; i++) for(var j = i + 1; j < custname.length + 1; j++) emit(custname.substring(i, j),doc._id); } }
>
> The view will contain all available sub-strings for custdes (e.g. custdes=abc -> a, ab, abc, bc) as key and doc._id as its value.
>
> After the view is created I can query it with the following http requests:
>
> http://127:0.0.1:5984/t-customers/_design/t-cust-design/_view/by-custdes?key="ab"
> http://127:0.0.1:5984/t-customers/_design/t-cust-design/_view/by-custdes?key="abc"
>
> It works as fast as lightning although my view has about 1.500.000 indexes.
>
> First of all: I've noticed that the PochdbBD syncs only the t-customers database and not it's view. Why? To make the view avaliable in PouchDB it requires for me to run the following command which takes up to 20 minutes to complete:
>
> t-customers.query("t-cust-design/by-custdes").then(...).catch(...);
>
> Only and only then I can see the view at IndexedDB in Chrome.

The first time you query it's when the view is build, now on the following times it should be much faster, since the rows are already indexed. This works just like CouchDB.

>
> Second of all: What is the way to lookup for doc in PouchDB view t-cust-design/by-custdes without triggering the whole map/reduce process every time I want to find the ab key? As I mentioned I can query the CouchDB _design/t-cust-design/_view/by-custdes view with http request and it works fast, but I'm unable to do the equivalent action using PouchDB API.
>

Do you mean you want to pass a "key" parameter like you do with CouchDB? You can pass that to the query function, like db.query('ddocname/viewname', {key: ...}).

> I've read tons of documentation but I'm still confused about it...
>

> --
> You received this message because you are subscribed to the Google Groups "PouchDB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pouchdb+u...@googlegroups.com.
> To post to this group, send email to pou...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pouchdb/77f34f68-ab4f-49fb-af3a-186f6f91ea83%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages