我在使用pouchdb-find创建用于在pouchDB中搜索的索引时遇到了一些麻烦。我一直收到错误couldn't find a usable index. try creating an index on: dateCreated. Make sure that only $eq, $gt, $gte, $lt, and $lte are used for the indexed fields.
- 但我不完全确定这意味着什么或如何修复它。
这是我的代码:
PouchDB.debug.enable('pouchdb:find');
var testDB = new PouchDB('testDB');
testDB.destroy('testDB').then(function () {
testDB = new PouchDB('testDB');
}).then(function () {
return testDB.bulkDocs([
{siteTitle : 'Google Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'google.com', _id: 'doc1'},
{siteTitle : 'Google Mail', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'google.com', _id: 'doc2'},
{siteTitle : 'Yahoo Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'yahoo.com', _id: 'doc3'},
{siteTitle : 'Bing Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'bing.com', _id: 'doc4'},
{siteTitle : 'Baidu Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'baidu.com', _id: 'doc5'},
{siteTitle : 'Hacker News', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'ycombinator.com', _id: 'doc6'},
{siteTitle : 'Mozilla foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'mozilla.org', _id: 'doc7'},
{siteTitle : 'Android foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'android.com', _id: 'doc8'},
{siteTitle : 'Wikipedia foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'wikipedia.org', _id: 'doc9'},
{siteTitle : 'Twitter foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'twitter.com', _id: 'doc10'},
{siteTitle : 'Facebook foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'facebook.com', _id: 'doc11'},
{siteTitle : 'Reddit foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'reddit.com', _id: 'doc12'},
{siteTitle : 'The Verge foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'theverge.com', _id: 'doc13'},
{siteTitle : 'Dropbox foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'dropbox.com', _id: 'doc14'},
{siteTitle : 'Microsoft foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'microsoft.com', _id: 'doc15'}
]);
}).then(function () {
return testDB.createIndex({
index: {
fields: ['dateCreated', 'domain'],
name: 'indexFoo'
}
});
}).then(function(result){
console.log(result);
}).then(function(){
return testDB.getIndexes();
}).then(function(result) {
console.log(JSON.stringify(result));
}).then(function(result){
return testDB.find({
selector: {
dateCreated: {'$exists': true}
},
use_index: 'indexFoo',
sort: [{dateCreated: 'desc'}]
});
}).then(function(result) {
console.log(result);
}).then(function(result) {
console.log(result);
}).catch(function (err) {
console.error(err);
console.log(err.message);
});
这是一个实时版本:https://jsbin.com/qimulecezi/8/edit?js,console
据我所知,我创建的索引与此处显示的方式相同:https://github.com/nolanlawson/pouchdb-find#dbcreateindexindex--callback 我对sort的使用几乎是逐字的:https://github.com/nolanlawson/pouchdb-find#sorting
答案 0 :(得分:2)
我认为我应该在这里发布答案,因为我发现它在其他地方。
nolanlawson回答了我关于irc的问题,他说:
$ exists不符合您的想法
你想要$ gt:null。对不起,这令人困惑。我没有设计API
答案 1 :(得分:0)
我在PouchDB中遇到了同样的错误“不可用的索引”。
在我的情况下,我创建了索引,然后运行db.find()并且它遇到了该错误。
要解决此问题,我更改了我的代码以运行搜索,并从索引回调调用另一个方法来执行查找。
acc col1 col2
100 12 13
100 13 14
100 17 23
100 22 109
100 23 110
100 29 130