get GroupBy count in Dexie

隐身守侯 提交于 2020-05-15 08:11:57

问题


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

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

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,
    ...
}

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


回答1:


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


来源:https://stackoverflow.com/questions/60912360/get-groupby-count-in-dexie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!