PouchDb 查找:为什么我的索引没有被使用?
PouchDb find : why is my index not used?
我正在使用 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 慢。
有什么想法吗?
$ne
目前没有使用任何索引。您将不得不使用 $gt
或 $lt
之类的东西。例如。而不是 $ne: 0
你会做 {$gt: 0, $lt: 0}
.
我正在使用 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 慢。
有什么想法吗?
$ne
目前没有使用任何索引。您将不得不使用 $gt
或 $lt
之类的东西。例如。而不是 $ne: 0
你会做 {$gt: 0, $lt: 0}
.