我使用DexieJS从IndexedDB中获取数据。我在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();发布于 2016-02-28 06:46:48
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对每个找到的项运行,这可能足够满足您的需要,也可能不够好。
至于如何使用复合索引,如果没有更多关于您想要的集合和准确查询的详细信息,就很难将其应用于您的确切情况,但是有这里有更多的信息。
https://stackoverflow.com/questions/35679590
复制相似问题