Indexeddb:使用通配符搜索 [英] Indexeddb: search using wildcards
问题描述
我想知道是否可以使用通配符在indexeddb对象存储上执行搜索. 例如,找到所有键都以"555"开头的对象都是很方便的
I was wondering if it is possible to perform a search on an indexeddb object store using wildcards. It would be handy to find all object with a key beginning with '555' for example
推荐答案
使用复合键或键片段可以立即使用. 键在IndexedDB中的工作方式是生成一个"keyRange"对象并将其传递给光标调用.键范围传递信息,例如起始于A,结束于Z,包括两端".
This is possible out of the box using either compound keys or key fragments. The way keys work in IndexedDB is that you generate a "keyRange" object and pass it to your cursor invocation. The keyrange passes info like "start at A and end at Z, inclusive."
从本质上讲,其中内置了部分匹配;缺点是您的光标将返回键之间的所有键,您可能需要进一步过滤掉这些结果.
By nature, there is partial matching built into this; the downside is that your cursor will return any keys that come between your keys and you might have to filter down those results further.
假设您将这些单词作为对象存储库中的键:
Say you have these words as keys in a object store:
- 土豚
- 苹果
- 微软
键范围"A到Z,含"将返回所有这些,而"Ap到Z,含"将仅返回最后三个.
The key range "A to Z, inclusive" would return all of these but "Ap to Z, inclusive" would return just the last three.
我用来实现此目的的另一种技术是将过滤器"函数传递给调用IndexedDB的方法.在方法onsuccess回调内部,将结果(event.target.result
)通过过滤器函数传递,如果结果返回true,则调用方法调用者的onsuccess回调.
Another technique I've used to implement this is by passing a "filter" function to my methods that invoke IndexedDB. Inside the methods onsuccess callback, pass the result (event.target.result
) through your filter function and if it returns true then call your method invoker's onsuccess callback.
这篇关于Indexeddb:使用通配符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!