如何使用复合键的IndexedDB计数方法? [英] How to use IndexedDB count method with composite keys?

查看:100
本文介绍了如何使用复合键的IndexedDB计数方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用keypath [feed_id,item_id]以及使用 os.put({feed_id:1,item_id:2,text: foo}); 可以正常工作。

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.

现在我想得到feed_id为13的所有feeditems的计数。问题在于即使是通常良好的MDN文档也可以在 https://developer.mozilla .org / en-US / docs / Web / API / IDBObjectStore 只能说关键参数是标识要计数的记录的键或键范围,但是 os .count({feed_id:13})失败,出现 DataError 异常参数不是有效密钥。。

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.".

那么,如何通过feed_id == x获得所有项目的计数(以及后面的代码,如何迭代)?

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

请注意,唯一性仅适用于feed_id和item_id - 可能有{entry_id:1,item_id:1}和{feed_id:2,item_id:1 }!

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}!

推荐答案

您仍然需要为 feed_id 你想查询。然后计算指数。复合索引与此查询无关。

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);

这篇关于如何使用复合键的IndexedDB计数方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆