0

I have an IDB object store "feeditems" with the keypath ["feed_id","item_id"], and inserting objects with os.put({feed_id:1,item_id:2,text:"foo"}); works fine.

Now I'd like to get a count of all feeditems where the feed_id is 13. The problem is that even the usually good MDN docs at https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore only say that the key parameter is "The key or key range that identifies the records to be counted.", yet a os.count({feed_id:13}) fails with a DataError exception "The parameter is not a valid key.".

So, how do I get the count of (and later, how do I iterate through) all the items with feed_id==x?

Note that the uniqueness is only combined for feed_id and item_id - there may very well be one entry with {feed_id:1,item_id:1} and one with {feed_id:2,item_id:1}!

Skynet
  • 558
  • 3
  • 16

1 Answers1

1

You still need to index the field, feed_id, that you want to query. An then count the index. Compound index is irrelevant for this query.

var index = objectStore.index('feed_id');
var req = index.count(IDBKeyRange.only(13);
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • Tried it on http://readme.fm/foo.php but it doesn't work - when looking in Chrome's Inspector, the "feeditems" object store has 6 items in it, but both indexes are empty :/ – Skynet Oct 31 '13 at 17:49
  • Oh, forget what I said. jquery.indexeddb is not quite consistent with the IndexedDB API specs (and did not support count with ranges). – Skynet Oct 31 '13 at 18:13