ash*_*ash 5 mongodb node.js aggregation-framework
我编写了一个从 MongoDB 数据库检索数据的 API。我还有一个使用来自 API 的数据的前端应用程序(如果相关的话,这两个应用程序都是使用 Koa 框架用 Node JS 编写的)
我需要对给定时期内需要计算(平均值、五分位数等)的大量数值数据进行聚合,这可能是按月、按年或按 personID 分组的所有数据。
我读过一些例子,人们说 API 应该用作数据库层的包装器,仅提供对原始数据的访问 - 但对我来说,逻辑将存在于数据库中(而不是询问前端应用程序来搅动数据)。
这是一个常见问题吗?根据您自己的经验,是使用 API 进行聚合更好,还是使用前端应用程序更好?
示例文档
{
"date": ISODate("2016-07-31T07:34:05+01:00Z"),
"value": 5,
"personID": 123
},
{
"date": ISODate("2016-08-01T12:53:05+01:00Z"),
"value": 3,
"personID": 789
}
Run Code Online (Sandbox Code Playgroud)
小智 2
您可以从两个角度来解决这个问题:安全性或性能
从安全角度来看,出于安全目的,您放置在前端的任何数据都被视为“脏”数据。这意味着,如果您接受任何输入,您就必须放弃任何输入有效的假设。特别是对于大型数据集,您需要对每个创建/更新操作进行某种形式的验证。虽然乍一看您可能认为将东西放在客户端可以减轻服务器的负载,但除非您希望到处都可以利用,否则您仍然会对数据进行某种迭代,即使只是为了验证它。
From the performance angle, moving large data sets to the client is going to happen either way, but same size doesn't need to come back. Keeping the operations on the server means your Update style operations are much smaller, as they don't need to move the entire data-set over wire but they can. To take it a step further, you can guarantee that at the very least you'll have control over the performance of the operations, where as if you offload this on the client, you're going to have to support every client's machine to some degree, which is a nightmare.
tl;dr: Security & Performance dictate heavily in favor of server side operations, especially on large data-sets.
| 归档时间: |
|
| 查看次数: |
5963 次 |
| 最近记录: |