IndexedDB - 搜尋



我們會遇到許多需要在物件儲存中搜索值的場景。物件儲存在內部已排序。可以透過以下方式進行:

  • 按鍵值或鍵範圍搜尋。
  • 基於其他物件欄位搜尋。

按鍵搜尋

我們可以使用 IDBKeyRange 物件和可接受的鍵範圍來搜尋精確的鍵值或鍵值範圍。IDBKeyRange 物件具有以下呼叫:

  • IDBKeyRange.lowerBound(lower, [open]) 用於 >= lower

  • IDBKeyRange.upperBound(upper, [open]) 用於 <= upper

  • IDBKeyRange.bound(lower, upper, [lowerOpen] , [upperOpen]) lower 和 upper 之間

  • IDBKeyRange.only(key) 如果範圍僅包含一個鍵。

為了執行實際搜尋,我們使用物件儲存上的查詢引數。執行這些操作的不同方法型別是

  • store.get(query) − 按鍵或範圍搜尋儲存中的第一個值

  • store.getAll([query],[count]) − 搜尋儲存中的所有值,直到達到指定的計數限制。

  • store.getKey(query) − 搜尋滿足查詢的第一個鍵。

  • store.getAllKeys([query],[count]) − 搜尋滿足查詢的所有鍵,直到達到計數限制。

  • store.count([query]) − 獲取滿足查詢的鍵的總數。

示例

在這個示例中,我們使用 getAll() 方法檢索所有物件,並按其鍵搜尋物件:

class.get(‘student’) 
class.getAll(IDBKeyRange.bound(‘science’,’math’) 
class.getAll(IDBKeyRange.upperbound(‘science’,true) 
class.getAll() 
class.getAllKeys(IDBKeyRange.lowerbound(‘student’,true))

按欄位或索引搜尋

要根據其他物件欄位進行搜尋,我們需要使用索引。索引儲存具有所需值的物件的鍵列表。索引也像物件儲存一樣在內部排序。

語法

objectStore.createIndex(name, keyPath, [options]);

name − 索引名稱

keyPath − 搜尋將在物件欄位的路徑上進行

options − 選項有兩種型別

  • unique − 儲存中具有唯一值的將在鍵路徑上存在物件,並且不能建立它們的副本。

  • multiEntry − 如果鍵路徑上的值是陣列,則預設情況下,索引會將整個陣列視為一個鍵,但如果我們使用 multiEntry,則陣列成員將成為索引鍵。

示例

如果我們想根據價格搜尋手機,示例程式如下:

openRequest.onupgradeneeded = function() { 
   let books = db.createObjectStore('phone', {keyPath: 'id'}); 
   let index = books.createIndex('pricephone', 'price'); 
};

要建立索引,我們需要使用升級。

  • 索引將跟蹤價格欄位。
  • 如果價格不唯一,則不能設定 unique 選項。
  • 如果價格不是陣列,則 multiEntry 不適用。

示例

在以下示例中,我們建立一個事務並使用 getAll() 函式檢索所有物件。檢索後,我們在該事務中搜索物件值。如果找到,則返回物件;如果沒有找到,則返回 false。

let transaction = db.transaction("phones"); 
let books = transaction.objectStore("phones"); 
let priceIndex = books.index("price_index");
let request = priceIndex.getAll(7); 
request.onsuccess = function() { 
   if (request.result !== undefined) { 
      document.write("Phones", request.result); 
   } else { 
      document.write("There are no such phones"); 
   } 
};

HTML 示例

下面給出了在物件儲存中搜索值的 HTML 指令碼實現:

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Document</title>
</head>
<body>
   <script>
      const request = indexedDB.open("botdatabase",1);
      request.onupgradeneeded = function(){
         const db = request.result;
         const store = db.createObjectStore("bots",{ keyPath: "id"});
         store.createIndex("branch_db",["branch"],{unique: false});
      }
      request.onsuccess = function(){
         document.write("database opened successfully");
         const db = request.result;
         const transaction=db.transaction("bots","readwrite");
         const store = transaction.objectStore("bots");
         const branchIndex = store.index("branch_db");
         store.add({id: 1, name: "jason",branch: "IT"});
         store.add({id: 2, name: "praneeth",branch: "CSE"});
         store.add({id: 3, name: "palli",branch: "EEE"});
         store.add({id: 4, name: "abdul",branch: "IT"});
         store.put({id: 4, name: "deevana",branch: "CSE"});
         const req = branchIndex.getAll(["CSE"]);
         req.onsuccess = function(){
            if(req.result!==undefined){
               document.write("bots",req.result);
            } else{
               document.write("There are no such bots");
            }
         };
         transaction.oncomplete = function(){
            db.close;
         }
      }
   </script>
</body>
</html>

輸出

database opened successfully
bots (2) [{...}, {...}]
arg1:(2) [{...}, {...}]
0:{id: 2, name: 'praneeth', branch: 'CSE'}
1:{id: 4, name: 'deevana', branch: 'CSE'}
length:2
[[Prototype]]:Array(0)
[[Prototype]]:Object
廣告
© . All rights reserved.