关于 PouchDB 的复杂 startkey/endkey 查询未返回预期结果
complex startkey/endkey query concerning PouchDB not returning expected result
我在 pouchdb 中创建了一个名为 session_indexes/by_lastModifiedDate_status
的索引,以发出时间戳和指示状态的字符串。
以下是映射函数 运行 数据库中有 6 个文档时发出的数组键:
[1404446400000, 'SUSPENDED']
[1409630400000, 'OPEN']
[1413777600000, 'OPEN']
[1423976400000, 'CLOSED']
[1425704400000, 'OPEN']
[1430193600000, 'OPEN']
现在,我想使用 startkey
和 endkey
查询该索引。我通过提供以下内容来做到这一点:
startkey: [1422766800000, 'CLOSED']
endkey: [1427688000000, 'CLOSED']
这意味着我要查找日期介于这两个时间戳之间且状态为 CLOSED
的所有文档。
但是,PouchDB 似乎 return 只有与日期匹配的结果 - 所以它是 returning 2 个结果(另一个是 [1425704400000, 'OPEN']
)。
我使用的地图功能如下。我知道它看起来很奇怪——但这实际上是由代码生成的。它不是人写的。但它仍然可以发出正确的键:
function(document) {
if(document._id.startsWith('session')) {
var keys = [];
if(document.lastModifiedDate) {
keys.push(document.lastModifiedDate);
}
if(document.status) {
keys.push(document.status.text.toUpperCase());
}
emit(keys, null);
}
}
查询本身如下:
return Database.instance().query('session_indexes/by_lastModifiedDate_status', {
startkey: [1422766800000, 'CLOSED'],
endkey: [1427688000000, 'CLOSED'],
include_docs: true
}).then(function(result) {
return _(result.rows).map(function(row) {
return Session.fromDocument(row.doc, new Session());
});
});
编辑:看来如果我颠倒顺序并将状态放在第一位,查询将按预期工作。请问为什么?我该如何解决?
如果您能为我提供任何帮助,我将不胜感激。谢谢!
由于 CouchDB 归类排序,它以这种方式工作。例如。对于数字+字母,你有:
[1, A]
[1, B]
[2, A]
[2, B]
等等
我在 pouchdb 中创建了一个名为 session_indexes/by_lastModifiedDate_status
的索引,以发出时间戳和指示状态的字符串。
以下是映射函数 运行 数据库中有 6 个文档时发出的数组键:
[1404446400000, 'SUSPENDED']
[1409630400000, 'OPEN']
[1413777600000, 'OPEN']
[1423976400000, 'CLOSED']
[1425704400000, 'OPEN']
[1430193600000, 'OPEN']
现在,我想使用 startkey
和 endkey
查询该索引。我通过提供以下内容来做到这一点:
startkey: [1422766800000, 'CLOSED']
endkey: [1427688000000, 'CLOSED']
这意味着我要查找日期介于这两个时间戳之间且状态为 CLOSED
的所有文档。
但是,PouchDB 似乎 return 只有与日期匹配的结果 - 所以它是 returning 2 个结果(另一个是 [1425704400000, 'OPEN']
)。
我使用的地图功能如下。我知道它看起来很奇怪——但这实际上是由代码生成的。它不是人写的。但它仍然可以发出正确的键:
function(document) {
if(document._id.startsWith('session')) {
var keys = [];
if(document.lastModifiedDate) {
keys.push(document.lastModifiedDate);
}
if(document.status) {
keys.push(document.status.text.toUpperCase());
}
emit(keys, null);
}
}
查询本身如下:
return Database.instance().query('session_indexes/by_lastModifiedDate_status', {
startkey: [1422766800000, 'CLOSED'],
endkey: [1427688000000, 'CLOSED'],
include_docs: true
}).then(function(result) {
return _(result.rows).map(function(row) {
return Session.fromDocument(row.doc, new Session());
});
});
编辑:看来如果我颠倒顺序并将状态放在第一位,查询将按预期工作。请问为什么?我该如何解决?
如果您能为我提供任何帮助,我将不胜感激。谢谢!
由于 CouchDB 归类排序,它以这种方式工作。例如。对于数字+字母,你有:
[1, A]
[1, B]
[2, A]
[2, B]
等等