gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:49 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com