尽管我想使用pouchdb-find 插件,但我找不到实现我需要的方法。相反,我使用了一种解决方法:
更改文档结构以将标签 ID 存储在数组中
{_id: 'record/1', name: 'Record 1', labels: ['label/1', 'label/2', 'label/3']},
// may not be sorted when being stored
{_id: 'record/2', name: 'Record 2', labels: ['label/1', 'label/5', 'label/7', 'label/3']},
{_id: 'record/3', name: 'Record 3', labels: ['label/2', 'label/3', 'label/4', 'label/5']}
创建设计文档
它将为每条记录发出多个复杂的键,以升序表示所有可能的标签映射。 map 函数将利用递归过程来生成密钥:
{
_id: '_design/records-with-labels',
views: {
'records-with-labels': {
map: function(doc) {
// important: sort them so that the required keys to be created are lesser
var labelIds = doc.labels.sort();
var lastIx = labelIds.length - 1;
var emitKey = function emitKey(currentKey, currentIx) {
console.log('emitting: ' + currentKey.join(',') + ' for ' + doc._id);
emit(currentKey, null);
var nextIx = currentIx + 1;
for (var jumpIx = nextIx + 1; jumpIx <= lastIx; jumpIx++) {
var jumpedLabelId = labelIds[jumpIx];
var jumpingKey = currentKey.concat([jumpedLabelId]);
console.log('emitting: ' + jumpingKey.join(',') + ' for ' + doc._id);
emit(jumpingKey, null);
}
if (nextIx > lastIx) {
return;
}
var nextLabelId = labelIds[nextIx];
currentKey.push(nextLabelId);
emitKey(currentKey, currentIx + 1);
};
labelIds.forEach(function(labelId, i) {
emitKey([labelId], i);
});
}.toString()
}
}
}
例如,文档record/1 将生成以下密钥:
emitting: label/1 for record/1
emitting: label/1,label/3 for record/1
emitting: label/1,label/2 for record/1
emitting: label/1,label/2,label/3 for record/1
emitting: label/2 for record/1
emitting: label/2,label/3 for record/1
emitting: label/3 for record/1
查询
我只需要确保查询标签按升序排序即可。
查询具有“label/1”和“label/3”的记录:
Db.query('records-with-labels', {
key: ['label/1', 'label/3']
});
查询具有“label/3”或“label/3”的记录:
Db.query('records-with-labels', {
keys: [['label/1'], ['label/3']]
});
这将为我们提供具有两个标签的重复记录,但 reduce 函数应该有助于消除它们。
结论
目前我不知道是否有更好的解决方案,但这对我来说已经足够了,因为就我而言,一条记录不会有太多标签。
如果您有更好的建议,请评论或编辑答案。