[英]aggregation on loopback mysql query
I want to know how to make aggregation on my loopback query I am using MySQL as the database I have a record like this in my DB - { 'app_name': 'chrome', 'duration' : 10000 }, { 'app_name': 'WhatsApp', 'duration' : 25000 } and so on
note that duration is in milliseconds. 我想知道如何在环回查询上进行聚合。我使用MySQL作为数据库,在数据库中有这样一条记录- { 'app_name': 'chrome', 'duration' : 10000 }, { 'app_name': 'WhatsApp', 'duration' : 25000 } and so on
,持续时间以毫秒为单位。 I am using angular 7 as front end and I want to make loopback query to sum the duration of my all records, right now I am making a query like - 我正在使用angular 7作为前端,并且我想进行环回查询以汇总所有记录的持续时间,现在我正在进行如下查询:
Apps.find({}).toPromise()
.then(apps => {
let result = apps.reduce((app, total) = {
total += app.duration;
return total
}, 0)
console.log(result);
})
.catch(err => {
console.log(err);
})
However, with this approach, I can get the sum of the duration but if I have millions of records than its not a scalable solution like iterating from millions of records and then sum the duration. 但是,使用这种方法,我可以得到持续时间的总和,但是如果我有数百万个记录,那么它不是一个可伸缩的解决方案,例如从数百万个记录进行迭代,然后求和持续时间。 I want to know if there is aggregation in loopback query for MySQL. 我想知道MySQL的回送查询中是否存在聚合。 I want a query like - 我想要一个查询-
Apps.find({
aggregation: {
sum: 'duration'
}
}).toPromise()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
something like that. 这样的事情。
LoopBack does not support aggregations yet. LoopBack还不支持聚合。 My recommendation is to write a custom controller method that will execute a custom SQL query to aggregate the results. 我的建议是编写一个自定义控制器方法,该方法将执行一个自定义SQL查询以聚合结果。
// in your controller
export class MyController {
constructor(
@repository(AppRepository) protected appRepo: AppRepository
) {}
// default CRUD methods scaffolded by lb4
// custom method
@get('/apps/durations')
getDurations() {
return this.appRepo.getAggregatedDurations();
}
}
// in your AppRepository
export class AppRepository extends DefaultCrudRepository<
App,
typeof App.prototype.id,
AppRelations
> {
constructor(
@inject('datasources.db') dataSource: juggler.DataSource,
// ...
) {
super(App, dataSource);
// ...
}
// add a new method
async getAggregatedDurations(): Promise<number> {
// A mock-up SQL query, you may need to tweak it
const result = await this.execute('SELECT SUM(duration) as total_duration FROM Apps');
// I am not entirely sure what's the shape of result object,
// you have to find out yourself how to access "total_duration" field
return result[0].total_duration;
}
}
See also API Docs for execute
method: https://loopback.io/doc/en/lb4/apidocs.repository.executablerepository.execute.html and LoopBack 3.x documentation: https://loopback.io/doc/en/lb3/Executing-native-SQL.html 另请参阅API文档以获取execute
方法: https : //loopback.io/doc/zh/lb/apidocs.repository.executablerepository.execute.html和LoopBack 3.x文档: https : //loopback.io/doc/zh/ LB3 /执行本地-SQL.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.