简体   繁体   English

PouchDB视图按键查找

[英]PouchDB view lookup by key

I have an CouchDB database named t-customers . 我有一个名为t-customers的CouchDB数据库。 Using Fauxton I've created the following view t-customers/_design/t-cust-design/_view/by-custdes . 使用Fauxton我创建了以下视图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 (eg custdes=abc -> a, ab, abc, bc) as key and doc._id as its value . 视图将包含custdes的所有可用子字符串(例如custdes = abc - > a,ab,abc,bc)作为doc._id作为其

After the view is created I can query it with the following http requests: 创建视图后,我可以使用以下http请求进行查询:

  • 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 documents indexed. 尽管我的观点有大约1.500.000个文档索引,但它的工作速度与闪电一样快。

First of all : I've noticed that the PouchBD syncs only the t-customers database and not it's view. 首先 :我注意到PouchBD只同步了t-customers数据库而不是它的视图。 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: 要在PouchDB中使视图可用,我需要运行以下命令,该命令最多需要20分钟才能完成:

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

Only and only then I can see the view at IndexedDB in Chrome. 只有这样我才能在Chrome中看到IndexedDB的视图。

Second of all : What is the way to look up a 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? 第二个 :在PouchDB查看t-cust-design/by-custdes查找文档的方法是什么,而不是每次想要找到ab键时都触发整个map / reduce进程? 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. 正如我所提到的,我可以使用http请求查询CouchDB _design/t-cust-design/_view/by-custdes视图,它运行速度很快,但我无法使用PouchDB API执行等效操作。

I've read tons of documentation but I'm still confused about it... 我已经阅读了大量的文档,但我仍然对此感到困惑......

The answer to your second question first (well, sort of): 首先回答你的第二个问题(好吧,有点):

To avoid generating all possible combinations of characters (leading to 1.500.000 view results), emit only whole words and query for keys starting with your query string. 为避免生成所有可能的字符组合(导致1.500.000视图结果),仅发出整个单词并查询以查询字符串开头的键。 You can use the startkey and endkey parameters for that. 您可以使用startkeyendkey参数。

function(doc) {
  var custname = doc.CUSTNAME || '';
  for(var i = 0; i < custname.length - 1; i++) {
    emit(custname.substring(i)); // You don't need to emit the _id - it's available on each view row automatically.
  }
}

Query the view with the parameters {startkey:'ab', endkey:'ab\￰'} . 使用参数{startkey:'ab', endkey:'ab\￰'}查询视图。 See the CouchDB docs for details. 有关详细信息,请参阅CouchDB文档

Regarding your first question: Views are always built per CouchDB and PouchDB instance. 关于您的第一个问题:视图始终根据CouchDB和PouchDB实例构建。 One of the reasons is that you can do filtered replications, so each instance might have its own view of the "world" aka database contents. 其中一个原因是您可以进行过滤复制,因此每个实例可能都有自己的“世界”即数据库内容视图。

I assume from your comments that you use PouchDB to replicate your database into the browser and then call the view locally, so technically you are using two instances of the database, which makes total sense if you're creating an offline-first app. 我从您的评论中假设您使用PouchDB将数据库复制到浏览器中,然后在本地调用视图,因此从技术上讲,您使用的是数据库的两个实例,如果您正在创建脱机优先应用程序,这将完全有意义。 But if you expect the CouchDB instance to always be available, just query the CouchDB server, so the views don't have to be rebuilt in each user's browser. 但是如果您希望CouchDB实例始终可用,只需查询CouchDB服务器,这样就不必在每个用户的浏览器中重建视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM