我在MongoDB中MongoDB的聚合 - 总和,其中
类似下面一个大的数据集我想运行在MongoDB中的聚集这将是该SQL相当于:
SELECT SUM(cores) from machines
WHERE idc='AMS' AND cluster='1' AND type='Physical';
我该如何去关于在MongoDB中进行此操作?
[
{
"_id" : "55d5dc40281077b6d8af1bfa",
"hostname" : "x",
"domain" : "domain",
"description" : "VMWare ESXi 5",
"cluster" : "1",
"type" : "Physical",
"os" : "EXSi",
"idc" : "AMS",
"environment" : "DR",
"deviceclass" : "host",
"cores" : "64",
"memory" : "256",
"mounts" : [ ],
"roles" : [
"ESX-HOST"
],
"ipset" : {
"backnet" : "1"
},
"frontnet" : [ ],
"created" : "2015-09-08T07:35:03.343Z"
},
{
"_id" : "55d5dc40281077b6d8af1bfb",
"hostname" : "x",
"domain" : "domain",
"description" : "VMWare ESXi 5",
"cluster" : "1",
"type" : "Physical",
"os" : "EXSi",
"idc" : "AMS",
"environment" : "DR",
"deviceclass" : "host",
"cores" : "64",
"memory" : "256",
"mounts" : [ ],
"roles" : [
"ESX-HOST"
],
"ipset" : {
"backnet" : "1"
},
"frontnet" : [ ],
"created" : "2015-09-08T07:35:03.346Z"
}
]
有手册中的网页的所有作为示例的通用操作:[SQL到聚合映射图](http://docs.mongodb.org/v3 0.0 /参考/ SQL-聚集比较/)。我建议你阅读它 –
你真的不需要为此聚合。我认为使用想要的是['.count'](http://docs.mongodb.org/manual/reference/method/db.collection.count/),因为它(SUM)(_id)在这里没有意义。 – styvane
对不起 - 错字 - 现在它应该更有意义 – Corbin