2014-12-08 44 views
3

我有以下的mysql查询,其中我已经完成了两个不同的字段,称为“count”和“population”,然后划分sum(count)/总和(人口),然后乘以100000,最后按年分组。如何在单独的mongodb查询中使用求和,乘法,除法和组合

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]}} 
     }]) 

任何人都可以指导我解决这个查询吗?

回答

6

我不确定“解决查询”的含义,但该查询在当前的表单中无效。我想你想要一个如下的管道:

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"] } ] } } } 
]) 

这是否回答你的问题?

+0

谢谢你的回答;它正在执行但没有显示任何结果。它正在显示 {“result”:[],“ok”:1} – Yash 2014-12-08 20:32:21

+0

这是什么意思? – Yash 2014-12-08 20:38:01