IndexedDB 获取Dexie中GroupBy计数

tp5buhyn  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(262)

我有一个IndexedDB表,它接受以下结构化JSON作为行:

{
    id : 1,
    name : 'doc1',
    createdDate : '2018-08-08'
}

我想在表中得到每个可用日期的计数。即:groupby:date then count。预期的示例输出格式为:

{
    '2018-08-08' : 5,
    '2018-08-18' : 19,
    ...
}

表中包含大量的记录。那么,如何使用Dexie高效地实现这一要求呢?

yrefmtwq

yrefmtwq1#

如果为createdDate编制索引,

const db = new Dexie("yourDB");
db.version(1).stores({
  yourTable: "id, name, createdDate"
});

然后,您可以执行以下操作:

const result = {}
await db.yourTable.orderBy('createdDate').eachKey(date => {
  result[date] = (result[date] || 0) + 1;
});

相关问题