IndexedDB中的多键查询(类似于sql中的OR)

Multiple keys query in IndexedDB (Similar to OR in sql)

本文关键字:sql 类似于 中的 OR 查询 IndexedDB      更新时间:2023-09-26

我在标签上使用multiEntry索引存储。

{ tags: [ 'tag1', 'tag2', 'tag3' ] }

和我有查询,也标签列表。

[ 'tag2', 'tag1', 'tag4' ]

我需要在查询中获得包含标签之一的所有记录(类似于SQL或语句)。

目前我找不到任何其他解决方案,除了在查询中遍历标签并按商店中的每个标签进行搜索。

有更好的解决方案吗?

谢谢。

您不能通过一个查询检索所有结果,除非使用迭代。您可以通过从最低值到最高值打开索引来优化搜索结果:

IDBKeyRange.bound ('tag1', 'tag4');

您可以使用的另一个Indexed-Db特性是打开多个查询并在查询完成时合并结果。这样会比迭代快得多。

IndexedDB只有范围查询作为Deni Mf回答。

OR查询是多个查询的简单联合。也许可以。

如果您想要高效的查询,您必须迭代游标并在必要时查找游标位置。使用我的库,它将是

tags = ['tag2', 'tag1', 'tag4'];
tags.sort();
iter = new ydn.db.KeyIterator('store name', 'tags', IDBKeyRange.bound(tags[0], tags[tags.length-1]);
keys = [];
i = 0; 
var req = db.open(iter, function(cursor) {
  if (tags.indexOf(cursor.indexKey()) >= 0) {
      // we got the result
     if (keys.indexOf(cursor.key()) == -1) { // remove duplicate
       keys.push(cursor.key());
     }
  } else {
     return tags[++i]; // jump to next index position.
  }
);
req.done(function() {
  db.list('store name', keys).done(function(results) {
     console.log(results);
   }
});

注意,该算法没有假阳性检索。首先执行仅键查询,这样我们就不会浪费在反序列化上。只有当我们删除了所有主键后才能检索结果