lord_g
lord_g

Reputation: 95

IndexedDb get all occurrences in index

In IndexedDb I have an index like this:

store.createIndex('distinctEqual', 'distinctEqualString', {unique: false});

When I do:

var tx = db.transaction(dbSettings.objectStore, "readwrite");
var store = tx.objectStore(dbSettings.objectStore);
var index = store.index('distinctEqual');
var reqIndex = index.get("pippo");

Is it possible to get all occurrences of "pippo" without using cursor in that IndexedDb index?

EDIT

If i do:

var cursorIndex =       
index.openKeyCursor(IDBKeyRange.only(currItem.distinctEqualString));
cursorIndex.onsuccess = function(evt) {
    //Can I know how many elements are there in cursorIndex?
};

As in a comment, can I know how many elements are there in cursorIndex?

Upvotes: 3

Views: 1384

Answers (2)

Kristof Degrave
Kristof Degrave

Reputation: 4180

The get will only return the first item that meets the keyrange. If you want to retrieve all, you can use the cursor like you mentioned. If you want to know the number of items you can use the count method.

Upvotes: 1

Josh
Josh

Reputation: 18690

You will want to use a cursor. This is the reason for using a cursor.

Upvotes: 1

Related Questions