1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
---
title: IDBIndex
slug: Web/API/IDBIndex
tags:
- API
- Database
- IDBIndex
- IndexedDB
- Interface
- NeedsTranslation
- Reference
- Storage
- TopicStub
translation_of: Web/API/IDBIndex
---
<p>{{APIRef("IndexedDB")}}</p>
<p><span class="seoSummary"><code>IDBIndex</code> 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.</span></p>
<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="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")}}</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. 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="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.getAll()")}}</dt>
<dd>Returns an {{domxref("IDBRequest")}} object, in a separate thread, finds all matching values in the referenced object store that correspond to the given key or are in range, if <code>key</code> is an {{domxref("IDBKeyRange")}}.</dd>
<dt>{{domxref("IDBIndex.getAllKeys()")}}</dt>
<dd>Returns an {{domxref("IDBRequest")}} object, in a separate thread, finds all matching keys in the referenced object store that correspond to the given key or are in range, 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>
<h2 id="Example">Example</h2>
<p>In the following example we open a transaction and an object store, then get the index <code>lName</code> 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 <code>ObjectStore</code> 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 <a href="https://mdn.github.io/indexeddb-examples/idbindex/">IDBIndex-example demo repo</a> (<a href="https://github.com/mdn/indexeddb-examples/tree/master/idbindex">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 = '<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.');
}
};
};</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> </td>
</tr>
<tr>
<td>{{SpecName("IndexedDB 2", "#index-interface", "IDBIndex")}}</td>
<td>{{Spec2("IndexedDB 2")}}</td>
<td> </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>Edge</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)}} (unprefixed)<br>
{{CompatChrome(38)}} (prefixes deprecated)<br>
{{CompatChrome(57)}} (prefixes removed)</td>
<td>{{CompatVersionUnknown}}</td>
<td>10 {{property_prefix("moz")}}<br>
{{CompatGeckoDesktop("16.0")}}</td>
<td>10, partial</td>
<td>{{CompatOpera(15)}}<br>
{{CompatOpera(44)}} (prefixes removed)</td>
<td>7.1</td>
</tr>
<tr>
<td><code>count()</code></td>
<td>{{CompatChrome(23.0)}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatGeckoDesktop("22.0") }}</td>
<td>10, partial</td>
<td>{{CompatOpera(15)}}</td>
<td>7.1</td>
</tr>
<tr>
<td><code>getAll()</code> and <code>getAllKeys()</code></td>
<td>{{CompatChrome(48.0)}}</td>
<td>{{CompatNo}}</td>
<td>{{ CompatGeckoDesktop("24.0") }}<br>
behind <code>dom.indexedDB.experimental</code> pref</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
</tr>
<tr>
<td>Available in workers</td>
<td>{{CompatVersionUnknown}} (unprefixed)<br>
{{CompatChrome(38)}} (prefixes deprecated)<br>
{{CompatChrome(57)}} (prefixes removed)</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("37.0")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}<br>
{{CompatOpera(44)}} (prefixes removed)</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td><code>isAutoLocale</code> and <code>locale</code></td>
<td>{{ CompatNo() }}</td>
<td>{{CompatNo}}</td>
<td>
<p>{{CompatGeckoDesktop("43.0")}}<br>
behind <code>dom.indexedDB.experimental</code> pref</p>
</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
</tr>
<tr>
<td><code>name</code> setter available</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop(49)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td>Indexed Database 2.0</td>
<td>{{CompatChrome(58)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatOpera(45)}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android Webview</th>
<th>Chrome for Android </th>
<th>Edge</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>{{CompatVersionUnknown}}<br>
{{CompatChrome(38)}} (prefixes deprecated)<br>
{{CompatChrome(57)}} (prefixes removed)</td>
<td>{{CompatVersionUnknown}}<br>
{{CompatChrome(38)}} (prefixes deprecated)<br>
{{CompatChrome(57)}} (prefixes removed)</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatGeckoMobile("22.0") }}</td>
<td>1.0.1</td>
<td>10</td>
<td>{{CompatOperaMobile(22)}}<br>
{{CompatOperaMobile(44)}} (prefixes removed)</td>
<td>{{ CompatNo() }}</td>
</tr>
<tr>
<td><code>count()</code></td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatGeckoMobile("22.0") }}</td>
<td>1.0.1</td>
<td>10</td>
<td>{{CompatOperaMobile(22)}}</td>
<td>{{ CompatNo() }}</td>
</tr>
<tr>
<td><code>getAll()</code> and <code>getAllKeys()</code></td>
<td>{{CompatChrome(48.0)}}</td>
<td> </td>
<td>{{CompatNo}}</td>
<td>{{ CompatGeckoMobile("24.0") }}<br>
behind <code>dom.indexedDB.experimental</code> pref</td>
<td>1.1 behind<br>
<code>dom.indexedDB.experimental</code> pref</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
</tr>
<tr>
<td>Available in workers</td>
<td>{{CompatVersionUnknown}} (unprefixed)<br>
{{CompatChrome(38)}} (prefixes deprecated)<br>
{{CompatChrome(57)}} (prefixes removed)</td>
<td>{{CompatVersionUnknown}} (unprefixed)<br>
{{CompatChrome(38)}} (prefixes deprecated)<br>
{{CompatChrome(57)}} (prefixes removed)</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("37.0")}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}<br>
{{CompatOperaMobile(44)}} (prefixes removed)</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td><code>isAutoLocale</code> and <code>locale</code></td>
<td>{{ CompatNo()}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatGeckoMobile("43.0")}}<br>
behind <code>dom.indexedDB.experimental</code> pref</td>
<td>2.5<br>
behind <code>dom.indexedDB.experimental</code> pref</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
<td>{{ CompatNo() }}</td>
</tr>
<tr>
<td><code>name</code> setter available</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile(49)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td>Indexed Database 2.0</td>
<td>{{CompatChrome(58)}}</td>
<td>{{CompatChrome(58)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatOperaMobile(45)}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</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="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>
|