Indexeddb在同一索引上搜索多个值
Indexeddb search multi values on same index
我在IndexedDB数据库中记录了一个JSON流,我不能在同一索引上搜索多个值的相等性。
My data:
datas = [
{name : 'Test P1', location : 'P1'},
{name : 'Test P1', location : 'P1'},
{name : 'Test P2', location : 'P2'},
{name : 'Test P2', location : 'P2'},
{name : 'Test P3', location : 'P3'},
{name : 'Test P3', location : 'P3'},
{name : 'Test P3', location : 'P3'}
]
数据库建设和请求:
// In onupgradeneeded
var objectStore = db.createObjectStore('entreprises', {keyPath: 'id_loc'});
objectStore.createIndex("name", "name");
objectStore.createIndex("location", "location");
//In query section
var transaction = db.transaction('entreprises','readonly');
var store = transaction.objectStore('entreprises');
var index = store.index('location');
// I want select only records where location = P1 and location = P2
var request = index.openCursor(IDBKeyRange.only(['P1', 'P2']));
// Select the first matching record
var request = index.get(IDBKeyRange.only(['P1', 'P2']));
查询找到了任何记录。这可能吗?
我怀疑你是否可以使用only()
的数组,API也没有这么说,检查下面的IDBKeyRange.only()。
也许这就是它不适合你的原因。
IDBKeyRange接口的only()方法创建一个新的键范围
因此,您可以为"P1"或"P2"(以行数较少的为准)打开游标,然后遍历游标以检查它是否包含其他所需的值。下面是我为你创建的一个方便参考的示例,所以可以调整它以删除硬编码值,如"location","P1","P2"等。
var resultArr = [];
var keyRange = IDBKeyRange.only("P1");
cursorHandler = indexHandler.openCursor(keyRange);
cursorHandler.onerror = function(event) {
if (errorCallBack && typeof(errorCallBack) == 'function') {
errorCallBack(event);
}
};
cursorHandler.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
if(cursor.value != null && cursor.value != undefined){
if(cursor.value["location"] == "P2"){
resultArr.push(cursor.value);
}
}
cursor["continue"]();
} else{
var resultObj = {"rows" : resultArr};
if (successCallBack && typeof(successCallBack) == 'function') {
successCallBack(resultObj);
}
}
return;
};
通过为每个查询创建游标来使用多查询技术
index.get(IDBKeyRange.only('P1')).onsuccess = cursorHandler;
index.get(IDBKeyRange.only('P2')).onsuccess = cursorHandler;
cursorHandler
应该将结果存储在按主键排序的数组中。
可以试试这样做:
// ...
objectStore.createIndex('locIndex',['loc1','loc2']);`
// ...
var index = store.index('locIndex');
var request = index.openCursor(IDBKeyRange.only(['P1', 'P2']));
相关文章:
- 在JavaScript中搜索文本并获取其所有开始和结束索引
- 如何有效地搜索具有介于 jquery 之间的索引的 name 属性的输入
- 在 2D Javascript 数组中搜索值索引
- 替代使用搜索或索引
- 索引数组 JavaScript 中索引的搜索值
- ElasticSearch - 从 Javascript 搜索 3 个索引
- 如何通过搜索选择HTML表的行索引
- Javascript:使用多个索引搜索indexeddb
- 用于jqGrid搜索的任意索引(列名)
- 从索引页获取搜索位置缩放到地图页面上的位置
- 如何搜索多个索引(es)相同的值在javascript数组
- 使用javascript搜索有限的手动索引
- 使用数组索引搜索字符串字母
- 如何通过文本搜索返回JSON数组中项目的索引位置?
- 如何在浏览器中高效地为索引搜索散列图像
- 通过选项文本搜索选项索引
- 在javascript中搜索并找到多维数组中字符串的索引
- 如何使用搜索或匹配自动填充拼接(索引开始、结束)
- 在IndexedDB中搜索复合索引
- 在javascript中搜索ajax请求元素的索引