IDBKeyRange

IDBKeyRange interface of the IndexedDB API represents a continuous interval over some data type that is used for keys. Records can be retrieved from IDBObjectStore and IDBIndex objects using keys or a range of keys. You can limit the range using lower and upper bounds. For example, you can iterate over all values of a key in the value range A–Z.

A key range can be a single value or a range with upper and lower bounds or endpoints. If the key range has both upper and lower bounds, then it is bounded ; if it has no bounds, it is unbounded . A bounded key range can either be open (the endpoints are excluded) or closed (the endpoints are included). To retrieve all keys within a certain range, you can use the following code constructs:

Range Code
All keys ≥ x IDBKeyRange.lowerBound ( x )
All keys > x IDBKeyRange.lowerBound ( x , true)
All keys ≤ y IDBKeyRange.upperBound ( y )
All keys < y IDBKeyRange.upperBound ( y , true)
All keys ≥ x && ≤ y IDBKeyRange.bound ( x , y )
All keys > x &&< y IDBKeyRange.bound ( x , y , true, true)
All keys > x && ≤ y IDBKeyRange.bound ( x , y , true, false)
All keys ≥ x &&< y IDBKeyRange.bound ( x , y , false, true)
The key = z IDBKeyRange.only ( z )

A key is in a key range if the following conditions are true:

  • The lower value of the key range is one of the following:
    • undefined
    • Less than key value
    • Equal to key value if lowerOpen is false .
  • The upper value of the key range is one of the following:
    • undefined
    • Greater than key value
    • Equal to key value if upperOpen is false .
注意: 此特征可用于 Web 工作者 .

特性

IDBKeyRange.lower 只读
Lower bound of the key range.
IDBKeyRange.upper 只读
Upper bound of the key range.
IDBKeyRange.lowerOpen 只读
Returns false if the lower-bound value is included in the key range.
IDBKeyRange.upperOpen 只读
Returns false if the upper-bound value is included in the key range.

方法

静态方法

IDBKeyRange.bound()
Creates a new key range with upper and lower bounds.
IDBKeyRange.only()
Creates a new key range containing a single value.
IDBKeyRange.lowerBound()
Creates a new key range with only a lower bound.
IDBKeyRange.upperBound()
Creates a new upper-bound key range.

实例方法

IDBKeyRange.includes()
Returns a boolean indicating whether a specified key is inside the key range.

范例

The following example illustrates how you'd use a key range. Here we declare a keyRangeValue as a range between values of "A" and "F". We open a transaction (using IDBTransaction ) and an object store, and open a Cursor with IDBObjectStore.openCursor , declaring keyRangeValue as its optional key range value. This means that the cursor will only retrieve records with keys inside that range. This range includes the values "A" and "F", as we haven't declared that they should be open  bounds. If we used IDBKeyRange.bound("A", "F", true, true); , then the range would not include "A" and "F", only the values between them.

注意 : For a more complete example allowing you to experiment with key range, have a look at our IDBKeyRange-example repo ( view the example live too .)

function displayData() {
  var keyRangeValue = IDBKeyRange.bound("A", "F");
  var transaction = db.transaction(['fThings'], 'readonly');
  var objectStore = transaction.objectStore('fThings');
  objectStore.openCursor(keyRangeValue).onsuccess = function(event) {
    var cursor = event.target.result;
    if(cursor) {
      var listItem = document.createElement('li');
      listItem.innerHTML = '<strong>' + cursor.value.fThing + '</strong>, ' + cursor.value.fRating;
      list.appendChild(listItem);
      cursor.continue();
    } else {
      console.log('Entries all displayed.');
    }
  };
}
					

规范

规范 状态 Comment
Indexed Database API 2.0
The definition of 'IDBKeyRange' in that specification.
Recommendation 初始定义。
Indexed Database API Draft
The definition of 'IDBKeyRange' in that specification.
Recommendation 添加 includes() .

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
IDBKeyRange Chrome 24
24
23 — 57
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
bound Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
includes Chrome 52 Edge ≤18 Firefox 47 IE ? Opera 39 Safari 10.1 WebView Android 52 Chrome Android 52 Firefox Android Yes Opera Android 41 Safari iOS 10.3 Samsung Internet Android 6.0
lower Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
lowerBound Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
lowerOpen Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
only Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
upper Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
upperBound Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
upperOpen Chrome 24
24
23 — 24
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 12 Firefox 16
16
10 — 16
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE 部分支持 10 Opera 15 Safari 7 WebView Android Yes Chrome Android Yes Firefox Android 22 Opera Android 14 Safari iOS 8 Samsung Internet Android Yes
Available in workers Chrome Yes Edge ≤18 Firefox 37 IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android 37 Opera Android Yes Safari iOS ? Samsung Internet Android Yes

图例

完整支持
完整支持
部分支持
部分支持
兼容性未知
兼容性未知
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅