我在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