我下面有一个IndexedDB表,它接受以下结构化JSON作为行:
{
id : 1,
name : 'doc1',
createdDate : '2018-08-08'
}
我想获取表格中每个可用日期的计数。即:groupby:date then count。预期的示例输出格式为:
{
'2018-08-08' : 5,
'2018-08-18' : 19,
...
}
表包含大量记录。那么,如何使用Dexie有效地实现这一要求?
如果您索引createdDate,
const db = new Dexie("yourDB");
db.version(1).stores({
yourTable: "id, name, createdDate"
});
然后您可以执行以下操作:
// Get all unique dates:
const allDates = await db.yourTable.orderBy('createdDate').uniqueKeys();
// Get counts for each
const dateCounts = await Promise.all(allDates.map(date => db.yourTable.where({createdDate: date}).count()));
// Put together result:
const result = {};
allDates.forEach((date, i) => result[date] = dateCounts[i]);