Skip to content

Collection.keys()

David Fahlander edited this page May 10, 2016 · 12 revisions

Syntax

collection.keys(callback)

Parameters

callback: Function function (keysArray) { } optional

Callback Parameters

keysArray: Array Array of keys

Return Value

Promise

Performance Notice

Similar to Collection.primaryKeys(), this operation is faster than Collection.toArray() of several reasons. First because entire objects does not need to be instanciated (less data processsing). Secondly because the underlying database engine need not to do follow the primaryKey reference for each found item and load it (less disk IO).

Remarks

Given callback will recieve an array containing all keys of the index being indexed in the collection. Can only be used on indexes and not on primary keys.

If callback is omitted and operation succeeds, returned Promise will resolve with the result of the operation, calling any Promise.then() callback.

If callback is specified and operation succeeds, given callback will be called and the returned Promise will resolve with the return value of given callback.

If operation fails, returned promise will reject, calling any Promise.catch() callback.

Sample

// List all the first name of all my friends:
db.friends.orderBy('firstName').keys(function (firstNames) {
    alert ("My friends are: " + firstNames.join(','));
});

// List all shoeSizes that my friends have (duplicates removed):
db.friends.orderBy('shoeSize').uniqueKeys(function (shoeSizes) {
    alert ("My friends have the following shoe sizes: " + shoeSizes.join(','));
});

// Not possible to use keys(), uniqueKeys(), eachKey() or eachUniqueKey() when
// Collection instance is based on the primary key:
db.friends.orderBy(':id').keys() // Will fail!
Clone this wiki locally