gpt4 book ai didi

javascript - 使用 pouchdb-find 在 PouchDB 中创建可用索引

转载 作者:行者123 更新时间:2023-11-29 14:44:34 28 4
gpt4 key购买 nike

我在使用 pouchdb-find 创建索引以在 pouchDB 中搜索时遇到了一些麻烦.我一直收到错误 找不到可用的索引。尝试在以下位置创建索引:dateCreated。确保只有 $eq、$gt、$gte、$lt 和 $lte 用于索引字段。 - 但我不完全确定这意味着什么或如何修复它。

这是我的代码:

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我对 find 和 sort 的使用几乎是逐字逐句的:https://github.com/nolanlawson/pouchdb-find#sorting

最佳答案

我认为我应该在这里发布答案,因为我在其他地方找到了它。

nolanlawson在 irc 上回答了我的问题,他说:

$exists doesn't do what you think it does

You want $gt: null instead. Sorry it's confusing. I didn't design the API

关于javascript - 使用 pouchdb-find 在 PouchDB 中创建可用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com