Indexed DB use multiple indexes in one transaction

2,798 views
Skip to first unread message

Deni Spasovski

unread,
Sep 17, 2012, 11:08:24 AM9/17/12
to chromiu...@chromium.org
Is it possible to use multiple indexes when searching through index db?
Currently I iterate through elements with one index and compare the returned results for second value, and this can be slow especially if index is not unique enough and returns lot of results. And i was wondering if it's possible to use more than one index when opening cursor?

//current code used for opening cursor
var db = initOpenReq.result;
var transaction = db.transaction(objectStoreName, 'readonly');
var objectStore = transaction.objectStore(objectStoreName);
var index = objectStore.index(indexName);
var keyRange = IDBKeyRange.only(value);
var cursorRequest = index.openCursor(keyRange);

cursorRequest.onsuccess = function (event)
            {
                if (event.target.result)
                {  
                        if(event.target.result.value[keyPath2].indexOf(value2) == 0){ //comparing with second value
                            agregate.push(event.target.result.value); //if value satisfies both results add to list
                        }
                       
                        event.target.result['continue']();
                 }
            };

Alec Flett

unread,
Sep 17, 2012, 1:29:17 PM9/17/12
to Deni Spasovski, chromiu...@chromium.org
I think what you might be looking for is an index with a keypath that is an array. The individual components of the array can still use keypaths too:

index = objectStore.createIndex("foo", ["field1", "field2.subvalue"]);

then the index value of {"field1": "one", "field2": {"subvalue": "two"}} is ["one", "two"] and you can actually call index.openCursor(["one", "two"]);

This does mean you have to set up the indexes in advance of course...
Alec


--
You received this message because you are subscribed to the Google Groups "Chromium HTML5" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-html5/-/ALJlMkQ-ziwJ.
To post to this group, send email to chromiu...@chromium.org.
To unsubscribe from this group, send email to chromium-html...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-html5/?hl=en.

Message has been deleted

Deni Spasovski

unread,
Sep 18, 2012, 1:49:31 PM9/18/12
to chromiu...@chromium.org, Deni Spasovski
10x Alec

I only needed 2 fields not sub values and i use the index as following and it works.
index = objectStore.createIndex("foo", ["field1", "field2"]);

However it's not possible to use this same index when only one value is supplied when searching, with 2 values it works fine.
Reply all
Reply to author
Forward
0 new messages