javascript - DexieJS (indexedDB) 链接多个 .where 子句

标签 javascript where-clause indexeddb dexie

我正在使用 DexieJS从 IndexedDB 中获取数据。我已经使用 v. 1.1.0 和 1.2.0 完成了以下测试。

它非常适合简单查询,但遗憾的是我无法链接多个 where 子句。

首先,我试过这个

var collection = db[table];
collection = collection.where('Field').equals("1");
return collection.count();

那是有效的。 然后,我需要添加一个 where 子句,但前提是设置了给定值:

var collection = db[table];
collection = collection.where('Field').equals("1");
if(value) collection = collection.where('Field2').above(value);
return collection.count();

这个失败了。出于测试目的,我也尝试过:

var collection = db[table];
collection = collection.where('Field').equals("1")
.and('Field2').above(value);
return collection.count();

var collection = db[table];
collection = collection.where('Field').equals("1")
.and().where('Field2').above(value);
return collection.count();

var collection = db[table];
collection = collection.where('Field').equals("1")
.where('Field2').above(value);
return collection.count();

这些都不起作用。我开始认为这根本不可能,但既然方法 and() 存在,一定有办法!

PS 这行得通:

var collection = db[table];
collection = collection.where('Field2').above(value);
return collection.count();

最佳答案

DexieJS 的 AND 运算符被实现为过滤函数或复合索引。实现查询的简单方法是使用 filter 方法,例如;

var collection = db[table];
collection = collection
    .where('Field').equals("1")
      .and(function(item) { return item.Field2 > value });
return collection.count();

这意味着第一个过滤器将针对 IndexedDB 运行,附加条件将针对 DexieJS 找到的每个项目运行,这可能足以满足您的需求,也可能不够好。

至于如何使用复合索引,如果没有关于您想要的集合和确切查询的更多详细信息,很难适用于您的确切情况,但是有much more information available here .

关于javascript - DexieJS (indexedDB) 链接多个 .where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35679590/

相关文章:

javascript - JavaScript 中什么是通过引用/值传递的?

javascript - Google map 未显示在 Modal Ionic 中

javascript - 在 firefox/chrome 中禁用 history api

MySQL 多字段搜索使用 Like 子句

javascript - IndexedDB 和 Angular 应用程序上的大量插入

javascript - 我怎样才能在已经存在的indexedDB中的已经存在的objectStore中 `put`新值?

javascript - 使用 QUnit 测试 Angular.js

sql - 无法理解使用 where 和 group by 的子查询代码

javascript - 使用 MongoDB 在 $where 中传递参数

javascript - 文件系统访问 API : is it possible to store the fileHandle of a saved or loaded file for later use?