简体   繁体   English

IDBKeyRange按键查询,其中键为String

[英]IDBKeyRange querying by key, where key is String

The key of an object stored in IndexedDB is an Array of the format: 存储在IndexedDB中的对象的键是以下格式的Array

[int, String]

eg [1, "Foo"] , [9, "Bar"] 例如[1, "Foo"][9, "Bar"]

How do I query the database using the string part of the key? 如何使用键的字符串部分查询数据库? For example I want to fetch all records with key [:any, "Some key"] . 例如,我想使用键[:any, "Some key"]获取所有记录。

Looks like IDBKeyRange is used for integer ranges, but I couldn't find a method to build arbitrary queries on keys. 看起来IDBKeyRange用于整数范围,但是我找不到在键上建立任意查询的方法。

When you create your index, ensure you set multiEntry to true, ie: 创建索引时,请确保将multiEntry设置为true,即:

objectStore.createIndex("tags","tags", {unique:false,multiEntry:true});

You can then get a range using the only operator - assume 'tag' is what you are looking for: 然后,您可以使用唯一的运算符获取范围-假设您要查找的是“标签”:

var rangeTest = IDBKeyRange.only(tag);

And that should do it for you. 那应该为您做。 I've got a full blog entry on this here: http://www.raymondcamden.com/index.cfm/2012/8/10/Searching-for-array-elements-in-IndexedDB 我在此处有完整的博客文章: http : //www.raymondcamden.com/index.cfm/2012/8/10/Searching-for-array-elements-in-IndexedDB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM