MongoDB-聚合多行
假设以下聚合查询:MongoDB-聚合多行,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,假设以下聚合查询: Machine.aggregate( [ { $match : { $and: [ {"idc": req.query.idc }, {"customer":req.query.customer} ] } } ,{"$group":{_id: {"cluster":"$cluster","idc":"$idc","type":"$type"},"SumCores":{"$sum":"$cores"},"SumMemory": { "$sum":"$memory" }}}
Machine.aggregate( [ { $match : { $and: [ {"idc": req.query.idc }, {"customer":req.query.customer} ] } } ,{"$group":{_id: {"cluster":"$cluster","idc":"$idc","type":"$type"},"SumCores":{"$sum":"$cores"},"SumMemory": { "$sum":"$memory" }}}, { $sort : { idc : -1, cluster: 1 } } ]);
返回:
[
{
"_id": {
"cluster": 1,
"idc": "LH5",
"type": "Virtual"
},
"SumCores": 112,
"SumMemory": 384
},
{
"_id": {
"cluster": 1,
"idc": "LH5",
"type": "Physical"
},
"SumCores": 192,
"SumMemory": 768
},
{
"_id": {
"cluster": 1,
"idc": "LH8",
"type": "Virtual"
},
"SumCores": 232,
"SumMemory": 469
},
{
"_id": {
"cluster": 1,
"idc": "LH8",
"type": "Physical"
},
"SumCores": 256,
"SumMemory": 1024
}
]
是否有方法更改聚合以检索此所需输出:
[
{
"_id": {
"cluster": 1,
"idc": "LH5"
},
"Virtual": {
"SumCores": 112,
"SumMemory": 384
},
"Physical": {
"SumCores": 192,
"SumMemory": 768
}
},
{
"_id": {
"cluster": 1,
"idc": "LH8"
},
"Virtual": {
"SumCores": 232,
"SumMemory": 469
},
"Physical": {
"idc": "LH8",
"type": "Physical"
}
}
]
假设:
- 每个IDC/群集始终有一个物理和虚拟“对”
b) 接收现有数据并通过库和/或算法将其更改为此格式您在查询中已经做了所有正确的事情,因为您需要在现有级别上进行
$group
,以获得正确的总和。剩下的唯一一件事就是把所有这些都集中起来
就我个人而言,我会坚持将数组中的“对”作为最终输出:
Machine.aggregate([
{“$match”:{
“idc”:req.query.idc,“customer”:req.query.customer}
} ,
{“$group”:{
“_id”:{
“集群”:“$cluster”,
“idc”:“$idc”,
“类型”:“$type”
},
“SumCores”:{“$sum”:“$cores”},
“SumMemory”:{“$sum”:“$memory”}
}},
{“$组”:{
“_id”:{
“集群”:“$\u id.cluster”,
“idc”:“$\u id.idc”
},
“数据”:{
“$push”:{
“类型”:“$\u id.type”,
“SumCores”:“$SumCores”,
“SumMemory”:“$SumMemory”
}
}
}},
{“$sort”:{“\u id.idc”:-1,“\u id.cluster”:1}
]);
这将给你:
{
“_id”:{
“集群”:1,
“idc”:“LH8”
},
“数据”:[
{
“类型”:“虚拟”,
“SumCores”:232,
“总结”:469
},
{
“类型”:“物理”,
“SumCores”:256,
“SumMemory”:1024
}
]
}
{
“_id”:{
“集群”:1,
“idc”:“LH5”
},
“数据”:[
{
“类型”:“虚拟”,
“SumCores”:112,
“总结”:384
},
{
“类型”:“物理”,
“SumCores”:192,
“总结”:768
}
]
}
但如果确实需要,则可以从数组中筛选出匹配的元素,并将它们放入它们自己的属性中:
Machine.aggregate([
{“$match”:{
“idc”:req.query.idc,“customer”:req.query.customer}
} ,
{“$group”:{
“_id”:{
“集群”:“$cluster”,
“idc”:“$idc”,
“类型”:“$type”
},
“SumCores”:{“$sum”:“$cores”},
“SumMemory”:{“$sum”:“$memory”}
}},
{“$组”:{
“_id”:{
“集群”:“$\u id.cluster”,
“idc”:“$\u id.idc”
},
“数据”:{
“$push”:{
“类型”:“$\u id.type”,
“SumCores”:“$SumCores”,
“SumMemory”:“$SumMemory”
}
}
}},
{“$project”:{
“物理”:{
“$setDifference”:[
{“$map”:{
“输入”:“$data”,
“as”:“el”,
“在”:{
“$cond”:[
{“$eq”:[“$$el.type”,“Physical”]},
{
“SumCores”:“$$el.SumCores”,
“SumMemory”:“$$el.SumMemory”
},
假的
]
}
}},
[错误]
]
},
“虚拟”:{
“$setDifference”:[
{“$map”:{
“输入”:“$data”,
“as”:“el”,
“在”:{
“$cond”:[
{“$eq”:[“$$el.type”,“Virtual”]},
{
“SumCores”:“$$el.SumCores”,
“SumMemory”:“$$el.SumMemory”
},
假的
]
}
}},
[错误]
]
}
}},
{“$unwind”:“$Physical”},
{“$unwind”:“$VISUAL”},
{“$sort”:{“\u id.idc”:-1,“\u id.cluster”:1}
]);
这将给出您的结果:
{
“_id”:{
“集群”:1,
“idc”:“LH8”
},
“物理”:{
“SumCores”:256,
“SumMemory”:1024
},
“虚拟”:{
“SumCores”:232,
“总结”:469
}
}
{
“_id”:{
“集群”:1,
“idc”:“LH5”
},
“物理”:{
“SumCores”:192,
“总结”:768
},
“虚拟”:{
“SumCores”:112,
“总结”:384
}
}
但是第一种方法只是提供相同的基本数据,而不需要对结果进行额外的传递
无论如何,它实际上只是多了一个
$group
,将所有数据集合在一起,然后是可选的阶段(如果您真的必须使用该数据格式)。但是我个人会处理代码中任何需要处理的“对”的访问。这正是我需要的-我也同意你的建议:)只是一个简短的说明-我将如何在你的方法中访问“物理”总内核?@CMS以类似的方式,通过从数组中过滤出需要匹配的元素。或者根据数据的使用方式(即显示),我只需迭代列表创建部分