这种排序不存在索引 - couchDB
No index exists for this sort - couchDB
使用排序方法访问数据时出错
错误是
No index exists for this sort
创建索引代码为
db.createIndex({
index: { fields: ["timestamp", "upVote"] },
}).then(() => {
this.intitialDatafromDB(db);
});
查找函数是
db.find({
selector: { upVote: { $gt: 5 } },
sort: [{ upVote: "desc" }],
fields: ["news"],
}).then((result: any) => {
this.data = result.docs;
}).catch((err: any) => {
console.log(err);
});
您的查询失败的原因是索引字段的顺序很重要。
来自 pouchdb 文档Indexing on more than one field
One thing to note is that the order of these fields matters when
creating your index
通过这样指定索引
fields: ['timestamp','upVote']
索引看起来像这样
timestamp
upVote
1590399369500
3
1590399369600
4
1590399369700
1
1590399369700
2
1590399369700
3
1590399369800
1
注意时间戳 1590399369700
,以及辅助字段 upVote
的排序方式。
如果您的索引字段是这样排序的
fields: ['upVote','timestamp']
鉴于上述理论数据,该指数将如下所示
upVote
timestamp
1
1590399369700
1
1590399369800
2
1590399369700
3
1590399369500
3
1590399369700
4
1590399369600
您的查询会 return 您期望的结果,如下面的代码片段所示。我建议阅读 Map/reduce queries;掌握该文档中提出的概念将有助于更深入地理解为什么会这样。
const g_result = 'result';
const getEl = id => document.getElementById(id);
let db;
async function view() {
const view = getEl(g_result);
const result = await db.find({
selector: {
upVote: {
$gt: 5
},
},
sort: [{
'upVote': 'desc'
}],
fields: ['news','upVote']
}, );
view.innerText = JSON.stringify(result, undefined, 3);
}
// canned test documents
function getDocsToInstall() {
return [{
timestamp: 1590399369508,
upVote: 3,
news: "new item 1"
},
{
timestamp: 1590399248600,
upVote: 4,
news: "new item 2"
},
{
timestamp: 1590399248600,
upVote: 5,
news: "new item 3"
},
{
timestamp: 1590399248700,
upVote: 6,
news: "new item 4"
},
{
timestamp: 1590399248900,
upVote: 7,
news: "new item 5"
},
{
timestamp: 1590399249000,
upVote: 8,
news: "new item 6"
},
]
}
// init example db instance
async function initDb() {
db = new PouchDB('test', {
adapter: 'memory'
});
await db.bulkDocs(getDocsToInstall());
await db.createIndex({
index: {
fields: ['upVote', 'timestamp']
}
});
};
(async() => {
await initDb();
await view();
})();
https: //whosebug.com/questions/69122670/no-index-exists-for-this-sort-couchdb#
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb-7.1.1.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.find.min.js"></script>
<pre id='view'></pre>
<div style='margin-top:2em'></div>
<pre id='result'>
</pre>
使用排序方法访问数据时出错
错误是
No index exists for this sort
创建索引代码为
db.createIndex({
index: { fields: ["timestamp", "upVote"] },
}).then(() => {
this.intitialDatafromDB(db);
});
查找函数是
db.find({
selector: { upVote: { $gt: 5 } },
sort: [{ upVote: "desc" }],
fields: ["news"],
}).then((result: any) => {
this.data = result.docs;
}).catch((err: any) => {
console.log(err);
});
您的查询失败的原因是索引字段的顺序很重要。
来自 pouchdb 文档Indexing on more than one field
One thing to note is that the order of these fields matters when creating your index
通过这样指定索引
fields: ['timestamp','upVote']
索引看起来像这样
timestamp | upVote |
---|---|
1590399369500 | 3 |
1590399369600 | 4 |
1590399369700 | 1 |
1590399369700 | 2 |
1590399369700 | 3 |
1590399369800 | 1 |
注意时间戳 1590399369700
,以及辅助字段 upVote
的排序方式。
如果您的索引字段是这样排序的
fields: ['upVote','timestamp']
鉴于上述理论数据,该指数将如下所示
upVote | timestamp |
---|---|
1 | 1590399369700 |
1 | 1590399369800 |
2 | 1590399369700 |
3 | 1590399369500 |
3 | 1590399369700 |
4 | 1590399369600 |
您的查询会 return 您期望的结果,如下面的代码片段所示。我建议阅读 Map/reduce queries;掌握该文档中提出的概念将有助于更深入地理解为什么会这样。
const g_result = 'result';
const getEl = id => document.getElementById(id);
let db;
async function view() {
const view = getEl(g_result);
const result = await db.find({
selector: {
upVote: {
$gt: 5
},
},
sort: [{
'upVote': 'desc'
}],
fields: ['news','upVote']
}, );
view.innerText = JSON.stringify(result, undefined, 3);
}
// canned test documents
function getDocsToInstall() {
return [{
timestamp: 1590399369508,
upVote: 3,
news: "new item 1"
},
{
timestamp: 1590399248600,
upVote: 4,
news: "new item 2"
},
{
timestamp: 1590399248600,
upVote: 5,
news: "new item 3"
},
{
timestamp: 1590399248700,
upVote: 6,
news: "new item 4"
},
{
timestamp: 1590399248900,
upVote: 7,
news: "new item 5"
},
{
timestamp: 1590399249000,
upVote: 8,
news: "new item 6"
},
]
}
// init example db instance
async function initDb() {
db = new PouchDB('test', {
adapter: 'memory'
});
await db.bulkDocs(getDocsToInstall());
await db.createIndex({
index: {
fields: ['upVote', 'timestamp']
}
});
};
(async() => {
await initDb();
await view();
})();
https: //whosebug.com/questions/69122670/no-index-exists-for-this-sort-couchdb#
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb-7.1.1.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.find.min.js"></script>
<pre id='view'></pre>
<div style='margin-top:2em'></div>
<pre id='result'>
</pre>