本文介绍了在Dexie中获取GroupBy计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an IndexedDB table that follows accepts following structured JSON as a row:

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

我想获取表格中每个可用日期的计数.即: groupby:date然后计数.预期的示例输出格式为:

I want to get count for each available date in the table. ie: groupby:date then count. Expected example output is in the format of:

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

表包含大量记录.那么,如何使用Dexie有效地实现这一要求?

Table contains large number of records. So, how can efficiently achieve this requirement using Dexie?

推荐答案

如果您索引createdDate,

If you index createdDate,

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

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

Then you could do the following:

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

这篇关于在Dexie中获取GroupBy计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 09:50