mongodb - 如何制作 sum(field)-sum(field2) 并获得所有文档的单个结果
问题描述
我有一个 mongodb 集合,其中包含两个字段,amount
并且type
,在type
我有一个cash-in
,cash-out
或transfer
。我想使用包含(the sum of all cash-ins in each document) - (sum of all cash-outs in each document)
. ps:推送的每一行要么是兑现交易,要么是兑现每次我添加一个新文档时,该字段都应该自行聚合。有没有这样的东西
> if type="cash-in"
> mutate {
> add_field => {
> "cash-in" => "%{amount}"
> }
> }
> if type="cash-out"
> mutate {
> add_field => {
> "cash-out" => "%{amount}"
> }
> }
>
> mutate {
> add_field => {
> "total" => "sum%{cash-in}-sum%{cash-out}"
> }
> }
解决方案
推荐阅读
- android - 如何使用三星健康应用程序获取游泳数据
- node.js - node.js express应用程序中的ejs编译错误
- flutter - 如何使 Appbar 的 IconButton 在颤动中居中
- android - 如何将来自 url 的图像存储在 Room 数据库中(Java)
- android - 如何在 Android 中检测设备抖动。使用哪个 Android 服务?
- typescript - Typescript 是对象属性的类型
- .htaccess - .htaccess 文件 ([^\/]+) 不工作,但 ([1-9]+[0-9]*) 工作?
- c++ - 在 OpenCL 内核中使用 printf 的问题
- android-studio - 在 AMD CPU 上运行 Android Studio 模拟器时出现问题
- mysql - MYSQL视图很慢