PouchDb发现:为什么我的索引没用?

时间:2016-07-21 07:41:36

标签: pouchdb

我正在使用PouchDb和插件PouchDb-find在离子网络应用程序中查询我的本地数据库。在几乎每个用例中,我在查询时收到以下警告,而我创建了一个索引:

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

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

以下是一个例子:

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'始终存在并且是一个数组):

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

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

我还尝试手动创建索引并在前一种情况下使用query(),但这比使用PouchDb-find慢。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

$ne目前不使用任何索引。您必须使用$gt$lt之类的内容。例如。而不是$ne: 0,你会做{$gt: 0, $lt: 0}

相关问题