环回聚合 mysql 查询
aggregation on loopback mysql query
我想知道如何对我的环回查询进行聚合我正在使用 MySQL 作为数据库我在我的数据库中有这样的记录 -
{ 'app_name': 'chrome', 'duration' : 10000 }, { 'app_name': 'WhatsApp', 'duration' : 25000 } and so on
请注意,持续时间以毫秒为单位。我正在使用 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);
})
但是,通过这种方法,我可以获得持续时间的总和,但如果我有数百万条记录,那么它不是一个可扩展的解决方案,比如从数百万条记录中迭代,然后对持续时间求和。
我想知道 MySQL 的环回查询中是否存在聚合。
我想要一个查询 -
Apps.find({
aggregation: {
sum: 'duration'
}
}).toPromise()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
类似的东西。
LoopBack 尚不支持聚合。我的建议是编写一个自定义控制器方法,该方法将执行自定义 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;
}
}
另请参阅 API execute
方法的文档: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
我想知道如何对我的环回查询进行聚合我正在使用 MySQL 作为数据库我在我的数据库中有这样的记录 -
{ 'app_name': 'chrome', 'duration' : 10000 }, { 'app_name': 'WhatsApp', 'duration' : 25000 } and so on
请注意,持续时间以毫秒为单位。我正在使用 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);
})
但是,通过这种方法,我可以获得持续时间的总和,但如果我有数百万条记录,那么它不是一个可扩展的解决方案,比如从数百万条记录中迭代,然后对持续时间求和。 我想知道 MySQL 的环回查询中是否存在聚合。 我想要一个查询 -
Apps.find({
aggregation: {
sum: 'duration'
}
}).toPromise()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
类似的东西。
LoopBack 尚不支持聚合。我的建议是编写一个自定义控制器方法,该方法将执行自定义 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;
}
}
另请参阅 API execute
方法的文档: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