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 1034116 of IDBIndex.multiEntry

  • Revision slug: Web/API/IDBIndex/multiEntry
  • Revision title: IDBIndex.multiEntry
  • Revision id: 1034116
  • Created:
  • Creator: Brettz9
  • Is current revision? Yes
  • Comment

Revision Content

{{ APIRef("IndexedDB") }}

The multiEntry property of the {{domxref("IDBIndex")}} interface returns a boolean value that affects how the index behaves when the result of evaluating the index's key path yields an array.

This is decided when the index is created, using the {{domxref("IDBObjectStore.createIndex")}} method. This method takes an optional options parameter shoe multiEntry property is set to true/false.

{{AvailableInWorkers}}

Syntax

var myIndex = objectStore.index('index');
console.log(myIndex.multiEntry);

Value

A {{domxref("Boolean")}}:

Value Effect
true There is one record in the index for each item in an array of keys.
false There is one record for each key that is an array.

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.

The multi-entry status of the index is logged to the console: it should be returned as false.

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'); 
  console.log(myIndex.multiEntry);

  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', '#widl-IDBIndex-multiEntry', 'multiEntry')}} {{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}}

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>multiEntry</code></strong> property of the {{domxref("IDBIndex")}} interface&nbsp;returns a boolean value that affects how the index behaves when the result of evaluating the index's key path yields an array.</p>

<p>This is decided when the index is created, using the {{domxref("IDBObjectStore.createIndex")}} method. This method takes an optional options parameter shoe <code style="font-style: normal;">multiEntry</code> property is set to&nbsp;<code style="font-style: normal;">true</code>/<code style="font-style: normal;">false</code>.</p>

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

<h2 id="Syntax">Syntax</h2>

<pre class="brush: js" style="font-size: 14px;">
var myIndex = objectStore.index('index');
console.log(myIndex.multiEntry);</pre>

<h3 id="Value">Value</h3>

<p>A {{domxref("Boolean")}}:</p>

<table class="standard-table">
 <tbody>
  <tr>
   <th>Value</th>
   <th>Effect</th>
  </tr>
  <tr>
   <td>true</td>
   <td>There is one record in the index for each item in an array of keys.</td>
  </tr>
  <tr>
   <td>false</td>
   <td>There is one record for each key that is an array.</td>
  </tr>
 </tbody>
</table>

<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>The multi-entry status of the index is logged to the console: it should be returned as&nbsp;<code>false</code>.</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 style="font-size: 14px;">
function displayDataByIndex() {
  tableEntry.innerHTML = '';
  var transaction = db.transaction(['contactsList'], 'readonly');
  var objectStore = transaction.objectStore('contactsList');

  var myIndex = objectStore.index('lName'); 
  console.log(myIndex.multiEntry);

  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', '#widl-IDBIndex-multiEntry', 'multiEntry')}}</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>

<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