Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1027116 of IDBKeyRange

  • Revision slug: Web/API/IDBKeyRange
  • Revision title: IDBKeyRange
  • Revision id: 1027116
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("IndexedDB")}}

The IDBKeyRange interface of the IndexedDB API represents a continuous interval over some data type that is used for keys. Records can be retrieved from {{domxref("IDBObjectStore")}} and {{domxref("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 {{domxref("IDBKeyRange.upperBound")}}(x)
All keys < x {{domxref("IDBKeyRange.upperBound")}}(x, true)
All keys ≥ y {{domxref("IDBKeyRange.lowerBound")}}(y)
All keys > y {{domxref("IDBKeyRange.lowerBound")}}(y, true)
All keys ≥ x && ≤ y {{domxref("IDBKeyRange.bound")}}(x, y)
All keys > x &&< y {{domxref("IDBKeyRange.bound")}}(x, y, true, true)
All keys > x && ≤ y {{domxref("IDBKeyRange.bound")}}(x, y, true, false)
All keys ≥ x &&< y {{domxref("IDBKeyRange.bound")}}(x, y, false, true)
The key = z {{domxref("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.

{{AvailableInWorkers}}

Methods

{{domxref("IDBKeyRange.bound()")}}
Creates a new key range with upper and lower bounds.
{{domxref("IDBKeyRange.includes()")}}
Returns a boolean indicating whether a specified key is inside the key range.
{{domxref("IDBKeyRange.only()")}}
Creates a new key range containing a single value.
{{domxref("IDBKeyRange.lowerBound()")}}
Creates a new key range with only a lower bound.
{{domxref("IDBKeyRange.upperBound()")}}
Creates a new upper-bound key range.

Properties

{{domxref("IDBKeyRange.lower")}} {{readonlyInline}}
Lower bound of the key range.
{{domxref("IDBKeyRange.upper")}} {{readonlyInline}}
Upper bound of the key range.
{{domxref("IDBKeyRange.lowerOpen")}} {{readonlyInline}}
Returns false if the lower-bound value is included in the key range.
{{domxref("IDBKeyRange.upperOpen")}} {{readonlyInline}}
Returns false if the upper-bound value is included in the key range.

Examples

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 {{domxref("IDBTransaction")}}) and an object store, and open a Cursor with {{domxref("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.

Note: 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.');
      }
    };
  };

Specifications

Specification Status Comment
{{SpecName('IndexedDB', '#idl-def-IDBKeyRange', 'IDBKeyRange')}} {{Spec2('IndexedDB')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 23{{property_prefix("webkit")}}
24
10 {{property_prefix("moz")}}
{{CompatGeckoDesktop("16.0")}}
10, partial 15 7.1
Available in workers {{CompatVersionUnknown}} {{CompatGeckoMobile("37.0")}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 {{CompatGeckoMobile("22.0")}} 1.0.1 10 22 8
Available in workers {{CompatVersionUnknown}} {{CompatGeckoMobile("37.0")}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}}

Be careful in Chrome as it still implements the old specification along with the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

  • Using IndexedDB
  • Starting transactions: {{domxref("IDBDatabase")}}
  • Using transactions: {{domxref("IDBTransaction")}}
  • Setting a range of keys: {{domxref("IDBKeyRange")}}
  • Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}
  • Using cursors: {{domxref("IDBCursor")}}
  • Reference example: To-do Notifications (view example live.)

Revision Source

<p>{{APIRef("IndexedDB")}}</p>

<div>
<p>The <strong><code>IDBKeyRange</code></strong> interface of the <a href="/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a>&nbsp;represents a continuous interval over some data type that is used for keys. Records can be retrieved from {{domxref("IDBObjectStore")}} and {{domxref("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.</p>
</div>

<p>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 <em>bounded</em>; if it has no bounds, it is <em>unbounded</em>. 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:</p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col" width="185">Range</th>
   <th scope="col" width="603">Code</th>
  </tr>
  <tr>
   <td>All keys ≤ <strong>x</strong></td>
   <td>{{domxref("IDBKeyRange.upperBound")}}<code>(<strong>x</strong>)</code></td>
  </tr>
  <tr>
   <td>All keys &lt; <strong>x</strong></td>
   <td>{{domxref("IDBKeyRange.upperBound")}}<code>(<strong>x</strong>, true) </code></td>
  </tr>
  <tr>
   <td>All keys ≥<strong> y</strong></td>
   <td>{{domxref("IDBKeyRange.lowerBound")}}<code>(<strong>y</strong>)</code></td>
  </tr>
  <tr>
   <td>All keys &gt;<strong> y</strong></td>
   <td>{{domxref("IDBKeyRange.lowerBound")}}<code>(<strong>y</strong>, true)</code></td>
  </tr>
  <tr>
   <td>All keys ≥ <strong>x</strong> &amp;&amp; ≤ <strong>y</strong></td>
   <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>)</code></td>
  </tr>
  <tr>
   <td>All keys &gt; <strong>x</strong> &amp;&amp;&lt; <strong>y</strong></td>
   <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, true, true)</code></td>
  </tr>
  <tr>
   <td>All keys &gt; <strong>x</strong> &amp;&amp; ≤ <strong>y</strong></td>
   <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, true, false)</code></td>
  </tr>
  <tr>
   <td>All keys ≥ <strong>x</strong> &amp;&amp;&lt; <strong>y</strong></td>
   <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, false, true)</code></td>
  </tr>
  <tr>
   <td>The key = <strong>z</strong></td>
   <td>{{domxref("IDBKeyRange.only")}}<code>(<strong>z</strong>)</code></td>
  </tr>
 </thead>
</table>

<p>A key is in a key range if the following conditions are true:</p>

<ul>
 <li>The lower value of the key range is one of the following:
  <ul>
   <li><code>undefined</code></li>
   <li>Less than key value</li>
   <li>Equal to key value if <code>lowerOpen</code> is <code>false</code>.</li>
  </ul>
 </li>
 <li>The upper value of the key range is one of the following:
  <ul>
   <li><code>undefined</code></li>
   <li>Greater than key value</li>
   <li>Equal to key value if <code>upperOpen</code> is <code>false</code>.</li>
  </ul>
 </li>
</ul>

<p>{{AvailableInWorkers}}</p>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("IDBKeyRange.bound()")}}</dt>
 <dd>Creates a new key range with upper and lower bounds.</dd>
 <dt>{{domxref("IDBKeyRange.includes()")}}</dt>
 <dd>Returns a boolean indicating whether a specified key is inside the key range.</dd>
 <dt>{{domxref("IDBKeyRange.only()")}}</dt>
 <dd>Creates a new key range containing a single value.</dd>
 <dt>{{domxref("IDBKeyRange.lowerBound()")}}</dt>
 <dd>Creates a new key range with only a lower bound.</dd>
 <dt>{{domxref("IDBKeyRange.upperBound()")}}</dt>
 <dd>Creates a new upper-bound key range.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("IDBKeyRange.lower")}} {{readonlyInline}}</dt>
 <dd>Lower bound of the key range.</dd>
 <dt>{{domxref("IDBKeyRange.upper")}} {{readonlyInline}}</dt>
 <dd>Upper bound of the key range.</dd>
 <dt>{{domxref("IDBKeyRange.lowerOpen")}} {{readonlyInline}}</dt>
 <dd>Returns false if the lower-bound value is included in the key range.</dd>
 <dt>{{domxref("IDBKeyRange.upperOpen")}} {{readonlyInline}}</dt>
 <dd>Returns false if the upper-bound value is included in the key range.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<p>The following example illustrates how you'd use a key range. Here we declare a <code>keyRangeValue</code> as a range between values of "A" and "F". We open a transaction (using {{domxref("IDBTransaction")}}) and an object store, and open a Cursor with {{domxref("IDBObjectStore.openCursor")}}, declaring&nbsp;<code>keyRangeValue</code> 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 &nbsp;bounds. If we used&nbsp;<span style="background-color:rgb(250, 251, 252); font-family:consolas,monaco,andale mono,monospace; font-size:1rem; line-height:19px; white-space:pre; word-spacing:normal">IDBKeyRange.bound("A", "F", true, true);</span>, then the range would not include "A" and "F", only the values between them.</p>

<div class="note">
<p><strong>Note</strong>: For a more complete example allowing you to experiment with key range, have a look at our <a href="https://github.com/mdn/IDBKeyRange-example">IDBKeyRange-example</a> repo (<a href="https://mdn.github.io/IDBKeyRange-example/">view the example live too</a>.)</p>
</div>

<pre class="brush: js">
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 = '&lt;strong&gt;' + cursor.value.fThing + '&lt;/strong&gt;, ' + cursor.value.fRating;
        list.appendChild(listItem);  
          
        cursor.continue();
      } else {
        console.log('Entries all displayed.');
      }
    };
  };</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('IndexedDB', '#idl-def-IDBKeyRange', 'IDBKeyRange')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>23{{property_prefix("webkit")}}<br />
    24</td>
   <td>10 {{property_prefix("moz")}}<br />
    {{CompatGeckoDesktop("16.0")}}</td>
   <td>10, partial</td>
   <td>15</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4.4</td>
   <td>{{CompatGeckoMobile("22.0")}}</td>
   <td>1.0.1</td>
   <td>10</td>
   <td>22</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div class="warning">
<p>Be careful in Chrome as it still implements the old specification along with the new one. Similarly it still has the prefixed <code>webkitIndexedDB</code> property even if the unprefixed <code>indexedDB</code> is present.</p>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
 <li>Starting transactions: {{domxref("IDBDatabase")}}</li>
 <li>Using transactions: {{domxref("IDBTransaction")}}</li>
 <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
 <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
 <li>Using cursors: {{domxref("IDBCursor")}}</li>
 <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="https://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>
Revert to this revision