IDBKeyRange: upperBound() 静的メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

メモ: この機能はウェブワーカー内で利用可能です。

upperBound()IDBKeyRange インターフェイスのメソッドはで、上限のみを持つ新しいキーの範囲を生成します。既定では、端点を含む「閉」です。

構文

js
IDBKeyRange.upperBound(upper)
IDBKeyRange.upperBound(upper, open)

引数

upper

新しいキーの範囲の上限を指定します。

open 省略可

上限が端点を除くかどうかを表します。既定値は false です。

返値

新しく生成されたキーの範囲を表す IDBKeyRange です。

例外

DataError DOMException

引数 upper に割り当てられたキーが有効なキーでないとき投げられます。

以下の例は、上限が指定されたキーの範囲の使用法を示します。keyRangeValue = IDBKeyRange.upperBound("F"); と宣言します。これは、値 "F" のそれより前の全てを含む範囲です。(IDBTransaction により) トランザクションを開き、オブジェクトストアを開き、省略可能なキーの範囲の値を keyRangeValue として IDBObjectStore.openCursor によりカーソルを開きます。

IDBKeyRange.upperBound("F", true); を使用すると、値 "F" は範囲に含まれず、それより前の値のみが範囲に含まれます。

メモ: キーの範囲に関する実験ができるより完全な例は、IDBKeyRange-example リポジトリーを参照してください。(動く例も見る)

js
function displayData() {
  const keyRangeValue = IDBKeyRange.upperBound("F");

  const transaction = db.transaction(["fThings"], "readonly");
  const objectStore = transaction.objectStore("fThings");

  objectStore.openCursor(keyRangeValue).onsuccess = (event) => {
    const cursor = event.target.result;
    if (cursor) {
      const listItem = document.createElement("li");
      listItem.textContent = `${cursor.value.fThing}, ${cursor.value.fRating}`;
      list.appendChild(listItem);

      cursor.continue();
    } else {
      console.log("全項目を表示しました。");
    }
  };
}

仕様書

Specification
Indexed Database API 3.0
# ref-for-dom-idbkeyrange-upperbound①

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
upperBound() static method

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報