大网数据广告活动
Web 标准与前端开发

IDBIndex: getAllRecords() method

· 52CE 我爱测

The **`getAllRecords()`** method of the IDBIndex interface retrieves all records (including index keys, primary keys, and values) from the index.

IndexedDB

The getAllRecords() method of the IDBIndex interface retrieves all records (including index keys, primary keys, and values) from the index.

getAllRecords() effectively combines the functionality of IDBIndex.getAllKeys and IDBIndex.getAll by enumerating both primary keys and values at the same time. This combined operation enables certain data retrieval patterns to be significantly faster than alternatives such as iteration with cursors.

Syntax

getAllRecords()
getAllRecords(options)

Parameters

An options object whose properties can include:

  • query
    • : A key or an IDBKeyRange identifying the records to retrieve. If this value is null or not specified, the browser will use an unbound key range.
  • count
    • : The number of records to return. If this value exceeds the number of records in the query, the browser will retrieve only the queried records. If the value is less than 0 or greater than 2^32 - 1, a TypeError exception will be thrown.
  • direction
    • : An enumerated value specifying the direction in which the records are traversed. Possible values are:
      • next
        • : The records are traversed from the beginning, in increasing key order. This is the default value.
      • nextunique
        • : The records are traversed from the beginning, in increasing key order. For every key with duplicate records, only the record closest to the start is yielded.
      • prev
        • : The records are traversed from the end, in decreasing key order.
      • prevunique
        • : The records are traversed from the end, in decreasing key order. For every key with duplicate records, only the record closest to the start is yielded.

Return value

An IDBRequest object on which subsequent events related to this operation are fired.

If the operation is successful, the value of the request's IDBRequest.result property is an Array of IDBRecord instances representing all the records that match the given query, up to the number specified by count (if provided).

Each IDBRecord instance contains the following properties:

  • key
    • : A value representing the record's key in the index.
  • primaryKey
    • : A value representing the key of the record in the index's associated IDBObjectStore.
  • value
    • : A value representing the record's value.

Exceptions

This method may raise a DOMException of the following types:

  • InvalidStateError DOMException
    • : Thrown if the IDBIndex or its associated IDBObjectStore has been deleted or removed.
  • TransactionInactiveError DOMException
    • : Thrown if this IDBIndex's transaction is inactive.
  • TypeError
    • : Thrown if the count parameter is not between 0 and 2^32 - 1, inclusive.

Examples

Basic usage

This example queries an IDBIndex for up to 100 records whose lastName values come after "Smith", with results sorted in reverse order.

The code first creates a transaction on an IDBDatabase named db (omitting the code to open the database), and then uses it to get an IDBObjectStore containing a contacts list, and from that an IDBIndex on the lastName property. It then calls getAllRecords() on the index, returning an IDBRequest instance. Event listeners are added to this request for the success and error events. On success, the result event.target.result is logged (this is also available as request.result). This result contains an array of IDBRecord instances. Note that because this is a query on an IDBIndex, the key and primaryKey in each record may have different values: the key is the index key (here, the lastName), while the primaryKey is the record's key in the object store.

// Create a transaction on the database and use it to get the contained store
const transaction = db.transaction(["contactsList"], "readonly");
const objectStore = transaction.objectStore("contactsList");
const myIndex = objectStore.index("lastName");

const query = IDBKeyRange.lowerBound("Smith", true);

const request = myIndex.getAllRecords({
  query,
  count: 100,
  direction: "prev",
});

request.addEventListener("success", (event) => {
  const myRecords = event.target.result; // Array of IDBRecord instances
  console.log(myRecords);
});

request.addEventListener("error", (event) => {
  console.error("Error retrieving records:", event.target.error);
});

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。