【发布时间】:2015-06-04 21:01:03
【问题描述】:
我有一个想要使用 PouchDB 编写的动态查询,但我发现与 PouchDB 相比,我很难解释如何使用 SQL 数据库来实现这一点。
我在 PouchDB 中有许多“会话”类型的文档,需要对这种类型的文档进行复杂的搜索。我想过滤诸如标题(以提供的值开头的字符串)、状态、所有者、设备 ID、开始日期、结束日期和其他一些内容。
现在,我知道我可以发出一个数组键,例如:
document.views = {
by_title_status_owner: {
map: function(document) {
if(document._id.startsWith('session')) {
emit([
document.title.toUpperCase(),
document.status.text.toUpperCase(),
document.owner.refId
], null);
}
}.toString()
}
};
我的问题是我不知道如何使用startkey 和endkey 进行查询。我正在尝试这样的事情:
startkey: [
(filters.title || '').toUpperCase(),
(filters.status || '').toUpperCase(),
(filters.owner || '')
],
endkey: [
(filters.title || '').toUpperCase() + '\uffff',
(filters.status || '').toUpperCase() + '\uffff',
(filters.owner || '').toUpperCase() + '\uffff'
],
但它似乎只过滤 first 索引 - 这意味着它正确过滤了标题,但是当按状态或所有者搜索时,查询返回所有结果。
我认为问题在于我真的想提供不同的发射数组键并根据用户输入的内容提供不同的开始键/结束键组合,但这似乎很难用 PouchDB 做到。
例如,假设用户为他们的title 搜索输入了“Lig”,并为状态选择了CLOSED。这意味着 startkey 应该看起来像 ['LIG', 'CLOSED'],而 endkey 可能看起来像 ['LIG\uffff', 'CLOSED']。但这是否意味着我必须发出键索引的每一个排列来匹配这个动态的 startkey/endkey?
这是我的第一次尝试,我什至没有添加 2+ 参数发出的键.....
getAllByCriteria: function(filters) {
var startkey = [], endkey = [];
if(filters.title) {
startkey.push((filters.title || '').toUpperCase());
endkey.push((filters.title || '').toUpperCase() + '\uffff');
}
if(filters.status) {
startkey.push((filters.status).toUpperCase());
endkey.push((filters.status).toUpperCase());
}
if(filters.owner) {
startkey.push(filters.owner);
endkey.push(filters.owner);
}
return Database.instance().query('session_indexes/by_criteria', {
startkey: startkey,
endkey: endkey,
include_docs: true
}).then(function(result) {
return _(result.rows).map(function(row) {
return Session.fromDocument(row.doc, new Session());
});
});
},
和:
db.upsert('_design/session_indexes', function(document) {
document.views = {
by_criteria: {
map: function(document) {
if(document._id.startsWith('session')) {
emit([], null)
emit([document.title.toUpperCase()], null);
emit([document.status.text.toUpperCase()], null);
emit([document.owner.refId], null);
}
}.toString()
}
};
return document;
});
我真的很困惑。我会很感激任何帮助。
谢谢!
【问题讨论】:
标签: javascript couchdb pouchdb