【问题标题】:get GroupBy count in Dexie在 Dexie 中获取 GroupBy 计数
【发布时间】:2020-07-09 17:57:24
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: javascript indexeddb dexie


    【解决方案1】:

    如果你索引 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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2013-07-14
      • 2014-01-09
      • 2022-10-25
      • 1970-01-01
      • 2017-05-30
      相关资源
      最近更新 更多