Javascript DexieJS(indexedDB)链多个.where子句
我正在使用从IndexedDB获取数据。我用v做了以下两个测试。1.1.0和1.2.0 它对于简单的查询非常有效,但不幸的是,我无法链接多个where子句 首先,我试过这个Javascript DexieJS(indexedDB)链多个.where子句,javascript,where-clause,indexeddb,dexie,Javascript,Where Clause,Indexeddb,Dexie,我正在使用从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
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();
这些都不管用。我开始认为这根本不可能,但既然方法和()
存在,就一定有办法
PS这项工作:
var collection = db[table];
collection = collection.where('Field2').above(value);
return collection.count();
DexieJS的和运算符可以作为过滤函数或复合索引实现。实现查询的简单方法是使用filter方法,比如
var collection = db[table];
collection = collection
.where('Field').equals("1")
.and(function(item) { return item.Field2 > value });
return collection.count();
这意味着第一个过滤器将针对IndexedDB运行,而附加条件将针对DexieJS找到的每个项运行,这对于您所需要的可能不够好,也可能不够好
至于如何使用复合索引,如果没有更多关于集合的详细信息和您想要的确切查询,就很难应用到您的确切情况,但是确实有。是的,对不起,我忘了说我也尝试过这个
collection=collection.where('Field')。equals(“1”)。和('Field2')。上述(值)代码>实际上什么都没有,我得到了一个白色屏幕,甚至添加了.catch(function(err){console.log(err);})代码>。我必须说,我正在使用ngDexie进行angular,也许这是一个带有that@ghego1奇怪。现在我将删除此答案,并在有时间设置测试用例后返回。更新后,从内存中删除并混合了或(使用与where
相同类型的条件工作)和和(使用过滤函数或复合索引工作)得到了它,谢谢。然后,我担心我的想法无法实现,我将用另一种方式设计它:-)