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 1001145 of IDBIndex

  • Revision slug: Web/API/IDBIndex
  • Revision title: IDBIndex
  • Revision id: 1001145
  • Created:
  • Creator: jpmedley
  • Is current revision? No
  • Comment Update compatibility table.

Revision Content

{{APIRef("IndexedDB")}}

The IDBIndex interface of the IndexedDB API provides asynchronous access to an index in a database. An index is a kind of object store for looking up records in another object store, called the referenced object store. You use this interface to retrieve data.

You can retrieve records in an object store through the primary key or by using an index. An index lets you look up records in an object store using properties of the values in the object stores records other than the primary key

The index is a persistent key-value storage where the value part of its records is the key part of a record in the referenced object store. The records in an index are automatically populated whenever records in the referenced object store are inserted, updated, or deleted. Each record in an index can point to only one record in its referenced object store, but several indexes can reference the same object store. When the object store changes, all indexes that refers to the object store are automatically updated.

You can grab a set of keys within a range. To learn more, see {{domxref("IDBKeyRange")}}.

{{AvailableInWorkers}}

Methods

Inherits from: EventTarget

{{domxref("IDBIndex.count()")}}
Returns an {{domxref("IDBRequest")}} object, and in a separate thread, returns the number of records within a key range.
{{domxref("IDBIndex.get()")}}
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the value in the referenced object store that corresponds to the given key or the first corresponding value, if key is an {{domxref("IDBKeyRange")}}.
{{domxref("IDBIndex.getKey()")}}
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the given key or the primary key, if key is an {{domxref("IDBKeyRange")}}.
{{domxref("IDBIndex.openCursor()")}}
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a cursor over the specified key range.
{{domxref("IDBIndex.openKeyCursor()")}}
Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a cursor over the specified key range, as arranged by this index.

Methods planned for the future

These methods don't currently work in browsers, but are planned for the future.

{{domxref("IDBIndex.getAll()")}} {{ Non-Standard_inline() }}
Instantly retrieves all objects inside an {{domxref("IDBObjectStore")}}, setting them as the result of the request object.
{{domxref("IDBIndex.getAllKeys()")}} {{ Non-Standard_inline() }}
Instantly retrieves the keys of all objects inside an {{domxref("IDBObjectStore")}}, setting them as the result of the request object.

Properties

{{domxref("IDBIndex.isAutoLocale")}} {{readonlyInline}} {{ Non-Standard_inline() }}
Returns a {{domxref("Boolean")}} indicating whether the index had a locale value of auto specified upon its creation (see createIndex()'s optionalParameters.)
{{domxref("IDBIndex.locale")}} {{readonlyInline}} {{ Non-Standard_inline() }}
Returns the locale of the index (for example en-US, or pl) if it had a locale value specified upon its creation (see createIndex()'s optionalParameters.)
{{domxref("IDBIndex.name")}} {{readonlyInline}}
The name of this index.
{{domxref("IDBIndex.objectStore")}} {{readonlyInline}}
The name of the object store referenced by this index.
{{domxref("IDBIndex.keyPath")}} {{readonlyInline}}
The key path of this index. If null, this index is not auto-populated.
{{domxref("IDBIndex.multiEntry")}} {{readonlyInline}}
Affects how the index behaves when the result of evaluating the index's key path yields an array. If true, there is one record in the index for each item in an array of keys. If false, then there is one record for each key that is an array.
{{domxref("IDBIndex.unique")}} {{readonlyInline}}
If true, this index does not allow duplicate values for a key.

Example

In the following example we open a transaction and an object store, then get the index lName from a simple contacts database. We then open a basic cursor on the index using {{domxref("IDBIndex.openCursor")}} — this works the same as opening a cursor directly on an ObjectStore using {{domxref("IDBObjectStore.openCursor")}} except that the returned records are sorted based on the index, not the primary key.

Finally, we iterate through each record, and insert the data into an HTML table. For a complete working example, see our IDBIndex-example demo repo (View the example live.)

function displayDataByIndex() {
  tableEntry.innerHTML = '';
  var transaction = db.transaction(['contactsList'], 'readonly');
  var objectStore = transaction.objectStore('contactsList');

  var myIndex = objectStore.index('lName'); 
  myIndex.openCursor().onsuccess = function(event) {
    var cursor = event.target.result;
    if(cursor) {
      var tableRow = document.createElement('tr');
      tableRow.innerHTML =   '<td>' + cursor.value.id + '</td>'
                           + '<td>' + cursor.value.lName + '</td>'
                           + '<td>' + cursor.value.fName + '</td>'
                           + '<td>' + cursor.value.jTitle + '</td>'
                           + '<td>' + cursor.value.company + '</td>'
                           + '<td>' + cursor.value.eMail + '</td>'
                           + '<td>' + cursor.value.phone + '</td>'
                           + '<td>' + cursor.value.age + '</td>';
      tableEntry.appendChild(tableRow);  

      cursor.continue();
    } else {
      console.log('Entries all displayed.');    
    }
  };
};

Specification

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

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatChrome(23.0)}}{{property_prefix("webkit")}}
{{CompatChrome(24.0)}}
10 {{property_prefix("moz")}}
{{CompatGeckoDesktop("16.0")}}
10, partial 15 7.1
count() {{CompatChrome(23.0)}} {{ CompatGeckoDesktop("22.0") }} 10, partial 15 7.1
getAll() and getAllKeys() {{CompatChrome(48.0)}} {{ CompatGeckoDesktop("24.0") }}
behind dom.indexedDB.experimental  pref
{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Available in workers {{CompatVersionUnknown}} {{CompatGeckoDesktop("37.0")}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}}
isAutoLocale and locale {{ CompatNo() }}

{{CompatGeckoDesktop("43.0")}}
behind dom.indexedDB.experimental  pref

{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support 4.4 {{CompatVersionUnknown}} {{ CompatGeckoMobile("22.0") }} 1.0.1 10 22 {{ CompatNo() }} {{CompatVersionUnknown}}
count() 4.4 {{CompatVersionUnknown}} {{ CompatGeckoMobile("22.0") }} 1.0.1 10 22 {{ CompatNo() }} {{CompatVersionUnknown}}
getAll() and getAllKeys() {{ CompatNo() }} {{CompatChrome(48.0)}} {{ CompatGeckoMobile("24.0") }}
behind dom.indexedDB.experimental  pref
1.1 behind
dom.indexedDB.experimental  pref
{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{CompatChrome(48.0)}}
Available in workers {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("37.0")}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
isAutoLocale and locale {{ CompatNo() }} {{ CompatNo()}} {{CompatGeckoMobile("43.0")}}
behind dom.indexedDB.experimental  pref
2.5
behind dom.indexedDB.experimental  pref
{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

Be careful in Chrome as it still implements the old specification along 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>IDBIndex</code></strong> interface of the <a href="/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a> provides asynchronous access to an <a href="/en/IndexedDB/Basic_Concepts_Behind_IndexedDB#gloss_index" title="en/IndexedDB/Basic_Concepts_Behind_IndexedDB#gloss_index">index</a> in a database. An index is a kind of object store for looking up records in another object store, called the referenced object store. You use this interface to retrieve data.</p>
</div>

<p>You can retrieve records in an object store through the primary key or by using an index. An index lets you look up records in an object store using properties of the values in the object stores records other than the primary key</p>

<p>The index is a persistent key-value storage where the value part of its records is the key part of a record in the referenced object store. The records in an index are automatically populated whenever records in the referenced object store are inserted, updated, or deleted. Each record in an index can point to only one record in its referenced object store, but several indexes can reference the same object store. When the object store changes, all indexes that refers to the object store are automatically updated.</p>

<p><span style="line-height:1.5">You can grab a set of keys within a range. To learn more, see {{domxref("IDBKeyRange")}}.</span></p>

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

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

<p>Inherits from: <a href="/en/DOM/EventTarget" title="en/DOM/EventTarget">EventTarget</a></p>

<dl>
 <dt>{{domxref("IDBIndex.count()")}}</dt>
 <dd>Returns an {{domxref("IDBRequest")}} object, and in a separate thread, returns the number of records within a key range.</dd>
 <dt>{{domxref("IDBIndex.get()")}}</dt>
 <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the value in the referenced object store that corresponds to the given key or the first corresponding value, if <code>key</code> is an {{domxref("IDBKeyRange")}}.</dd>
 <dt>{{domxref("IDBIndex.getKey()")}}</dt>
 <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the given key or the primary key, if <code>key</code> is an {{domxref("IDBKeyRange")}}.</dd>
 <dt>{{domxref("IDBIndex.openCursor()")}}</dt>
 <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a <a href="/en/IndexedDB#gloss_cursor" title="en/IndexedDB#gloss cursor">cursor</a> over the specified key range.</dd>
 <dt>{{domxref("IDBIndex.openKeyCursor()")}}</dt>
 <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a cursor over the specified key range, as arranged by this index.</dd>
</dl>

<h3 id="Methods_planned_for_the_future">Methods planned for the future</h3>

<p>These methods don't currently work in browsers, but are planned for the future.</p>

<dl>
 <dt>{{domxref("IDBIndex.getAll()")}} {{ Non-Standard_inline() }}</dt>
 <dd>Instantly retrieves all objects inside an {{domxref("IDBObjectStore")}}, setting them as the&nbsp;<code>result</code>&nbsp;of the request object.</dd>
</dl>

<dl>
 <dt>{{domxref("IDBIndex.getAllKeys()")}} {{ Non-Standard_inline() }}</dt>
 <dd>Instantly retrieves the keys of all objects inside an {{domxref("IDBObjectStore")}}, setting them as the&nbsp;<code><a href="/en/IndexedDB/IDBRequest#attr_result" title="en/IndexedDB/IDBRequest#attr result">result</a></code>&nbsp;of the request object.</dd>
</dl>

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

<dl>
 <dt>{{domxref("IDBIndex.isAutoLocale")}} {{readonlyInline}} {{ Non-Standard_inline() }}</dt>
 <dd>Returns a {{domxref("Boolean")}} indicating whether the index had a <code>locale</code> value of <code>auto</code> specified upon its creation (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/createIndex#Parameters"><code>createIndex()</code>'s optionalParameters</a>.)</dd>
 <dt>{{domxref("IDBIndex.locale")}} {{readonlyInline}} {{ Non-Standard_inline() }}</dt>
 <dd>Returns the locale of the index (for example <code>en-US</code>, or <code>pl</code>) if it had a <code>locale</code> value specified upon its creation (see <a href="https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/createIndex#Parameters"><code>createIndex()</code>'s optionalParameters</a>.)</dd>
 <dt>{{domxref("IDBIndex.name")}} {{readonlyInline}}</dt>
 <dd>The name of this index.</dd>
 <dt>{{domxref("IDBIndex.objectStore")}} {{readonlyInline}}</dt>
 <dd>The name of the object store referenced by this index.</dd>
 <dt>{{domxref("IDBIndex.keyPath")}} {{readonlyInline}}</dt>
 <dd>The <a href="/en/IndexedDB#gloss_key_path" title="en/IndexedDB#gloss key path">key path</a> of this index. If null, this index is not <a href="/en/IndexedDB/Index#gloss_auto-populated" title="en/IndexedDB/Index#gloss auto-populated">auto-populated</a>.</dd>
 <dt>{{domxref("IDBIndex.multiEntry")}} {{readonlyInline}}</dt>
 <dd>Affects how the index behaves when the result of evaluating the index's key path yields an array. If <code>true</code>, there is one record in the index for each item in an array of keys.&nbsp;If <code>false</code>, then there is one record for each key that is an array.</dd>
 <dt>{{domxref("IDBIndex.unique")}} {{readonlyInline}}</dt>
 <dd>If <code>true</code>, this index does not allow duplicate values for a key.</dd>
</dl>

<h2 id="Example">Example</h2>

<p>In the following example we open a transaction and an object store, then get the index&nbsp;<code>lName</code>&nbsp;from a simple contacts database. We then open a basic cursor on the index using {{domxref("IDBIndex.openCursor")}}&nbsp;— this works the same as opening a cursor directly on an&nbsp;<code>ObjectStore</code>&nbsp;using {{domxref("IDBObjectStore.openCursor")}} except that the returned records are sorted based on the index, not the primary key.</p>

<p>Finally, we iterate through each record, and insert the data into an HTML table. For a complete working example, see our&nbsp;<a href="https://github.com/mdn/IDBIndex-example">IDBIndex-example demo repo</a>&nbsp;(<a href="https://mdn.github.io/IDBIndex-example/">View the example live</a>.)</p>

<pre class="brush: js" style="font-size: 14px;">
function displayDataByIndex() {
  tableEntry.innerHTML = '';
  var transaction = db.transaction(['contactsList'], 'readonly');
  var objectStore = transaction.objectStore('contactsList');

  var myIndex = objectStore.index('lName'); 
  myIndex.openCursor().onsuccess = function(event) {
    var cursor = event.target.result;
    if(cursor) {
      var tableRow = document.createElement('tr');
      tableRow.innerHTML =   '&lt;td&gt;' + cursor.value.id + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.lName + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.fName + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.jTitle + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.company + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.eMail + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.phone + '&lt;/td&gt;'
                           + '&lt;td&gt;' + cursor.value.age + '&lt;/td&gt;';
      tableEntry.appendChild(tableRow);  

      cursor.continue();
    } else {
      console.log('Entries all displayed.');    
    }
  };
};</pre>

<h2 id="Specification">Specification</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-IDBIndex', 'IDBIndex')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

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

<p>{{ CompatibilityTable() }}</p>

<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>{{CompatChrome(23.0)}}{{property_prefix("webkit")}}<br />
    {{CompatChrome(24.0)}}</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><code>count()</code></td>
   <td>{{CompatChrome(23.0)}}</td>
   <td>{{ CompatGeckoDesktop("22.0") }}</td>
   <td>10, partial</td>
   <td>15</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>getAll()</code> and <code>getAllKeys()</code></td>
   <td>{{CompatChrome(48.0)}}</td>
   <td>{{ CompatGeckoDesktop("24.0") }}<br />
    behind <code>dom.indexedDB.experimental</code>&nbsp; pref</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>isAutoLocale</code> and <code>locale</code></td>
   <td>{{ CompatNo() }}</td>
   <td>
    <p>{{CompatGeckoDesktop("43.0")}}<br />
     behind <code>dom.indexedDB.experimental</code>&nbsp; pref</p>
   </td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4.4</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoMobile("22.0") }}</td>
   <td>1.0.1</td>
   <td>10</td>
   <td>22</td>
   <td>{{ CompatNo() }}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>count()</code></td>
   <td>4.4</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoMobile("22.0") }}</td>
   <td>1.0.1</td>
   <td>10</td>
   <td>22</td>
   <td>{{ CompatNo() }}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>getAll()</code> and <code>getAllKeys()</code></td>
   <td>{{ CompatNo() }}</td>
   <td>{{CompatChrome(48.0)}}</td>
   <td>{{ CompatGeckoMobile("24.0") }}<br />
    behind <code>dom.indexedDB.experimental</code>&nbsp; pref</td>
   <td>1.1 behind<br />
    <code>dom.indexedDB.experimental</code>&nbsp; pref</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{CompatChrome(48.0)}}</td>
  </tr>
  <tr>
   <td>Available in workers</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>isAutoLocale</code> and <code>locale</code></td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo()}}</td>
   <td>{{CompatGeckoMobile("43.0")}}<br />
    behind <code>dom.indexedDB.experimental</code>&nbsp; pref</td>
   <td>2.5<br />
    behind <code>dom.indexedDB.experimental</code>&nbsp; pref</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div class="warning">
<p>Be careful in Chrome as it still implements the old specification along 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