PouchDb find:为什么不使用我的索引? [英] PouchDb find : why is my index not used?

查看:199
本文介绍了PouchDb find:为什么不使用我的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PouchDb和查找插件PouchDb在离子Web应用程序中查询我的本地数据库。在几乎每个用例中,在查询并创建索引时都会收到以下警告:

I'm using PouchDb and the plugin PouchDb-find to query my local DB in an ionic web app. In almost every use case, I get the following warning when querying whereas I've created an index:

{
    "docs": {
        ...
    },
    "warning": "no matching index found, create an index to optimize query time"
}

从插件文档的示例开始,我收到此警告,所以我想知道自己做错了什么。

Starting from an example of the plugin documentation, I get this warning so I am wondering what I am doing wrong.

这里是一个示例:

var db = new PouchDB('test');

var docs = [];
for (var i = 0; i < 10; i++) {
  docs.push({title: 'Lorem ipsum ' + i, _id: 'doc' + (i + 1)});
}

db.bulkDocs(docs)
  .then(
    function () {
      return db.createIndex({index: {fields: ['title']}});
    })
  .then(
    function () {
      // return db.find({selector: {title: {$eq: 0}}}); // No warning
      return db.find({selector: {title: {$ne: 0}}}); // Warning
    })
  .then(
    function (res) {
      console.log(res);
    })
  .catch(console.error.bind(console));

为什么在使用 $ eq 而不是 $ ne

然后,我在以下查询中遇到了更复杂的情况(假设'a.deep.property'始终存在并且是一个数组)

Then, I have a more complex case with the following query (assuming 'a.deep.property' always exists and is an Array) :

db.find(
    {
        selector: {
            $not:{
                "a.deep.property": {$size:0}
            }
        }
    }
);

我用字段'a.deep.property创建了一个索引'。索引也未使用。我需要创建什么索引?

I've created an index with the field 'a.deep.property'. The index is not used either. What index do I need to create ?

我也尝试手动创建索引并使用 query()

I also tried to create an index manually and to use query() in the previous case but this was slower than using PouchDb-find.

任何想法?

推荐答案

$ ne 当前不使用任何索引。您必须使用类似 $ gt $ lt 之类的东西。例如。而不是 $ ne:0 ,您应该执行 {$ gt:0,$ lt:0}

$ne currently does not use any index. You would have to use something like $gt or $lt instead. E.g. instead of $ne: 0 you would do {$gt: 0, $lt: 0}.

这篇关于PouchDb find:为什么不使用我的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆