首页 > 解决方案 > 如何制作 sum(field)-sum(field2) 并获得所有文档的单个结果

问题描述

我有一个 mongodb 集合,其中包含两个字段,amount并且type,在type我有一个cash-incash-outtransfer。我想使用包含(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}"
>   }
> }

标签: mongodblogstash-configuration

解决方案


推荐阅读