如何在单个mongodb查询中使用求和,乘法,除法和分组聚合

亚什

我有以下mysql查询,其中我在称为“ count”和“ population”的两个不同字段上完成了总和,然后将sum(count)/ sum(population)相除,然后乘以100000,最后按年份将其分组。

Select year, cname, (sum(count)/sum(population))*100000 as total from cancer c where c.cname ="lung and bronchus" group by year;

我在mongodb中编写了以下查询,但不确定如何投影cname和year。

db.cancer_stats.aggregate([
       {$match:{cname: "lung and bronchus"}},
       {$group:{_id:"year"},
               {total:{$multiply:[$divide:[$sum:"$count", $sum:"population"], 100000]}}
        }])

谁能指导我解决这个查询?

wdberkeley

我不确定“解决查询”的意思,但是该查询在当前形式下无效。我认为您想要一个类似以下的管道:

db.cancer_stats.aggregate([
    { "$match" : { "cname" : "lung and bronchus" } },
    { "$group" : { "_id" : "year", "t_count" : { "$sum" : "$count" }, "t_population" : { "$sum" : "$population" } } },
    { "$project" : { "result" : { "$multiply" : [100000, { "$divide" : ["$t_count", "$t_population"] } ] } } }
])

这是否回答你的问题?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章