0
我是MongoDB的一位相对新手,并且几天后我一直在为这个问题而苦苦挣扎。对嵌套文档进行分组
我已经做了很多尝试,并阅读所有可能的文档&关于类似需求的问题,但我还没有找到一个可能的解决方案。
我有如下记录结构:
{ _id: ObjectId("1234"),
locationName: "fakeaccount",
timeZone: "Europe/Rome",
metricValues: [
{
metric: "QUERIES_DIRECT",
totalValue: {
metricOption: "AGGREGATED_TOTAL",
timeDimension: {
timeRange: {
startTime: "2016-01-01T00:00:00Z",
endTime: "2016-01-03T00:00:00Z"
}
},
value: "0"
},
dimensionalValues: [
{
metricOption: "AGGREGATED_DAILY",
timeDimension: {
timeRange: {
startTime: "2016-01-01T23:00:00Z"
}
},
value: "0"
},
{
metricOption: "AGGREGATED_DAILY",
timeDimension: {
timeRange: {
startTime: "2016-01-02T23:00:00Z"
}
},
value: "1"
}
]
},
{
metric: "QUERIES_INDIRECT",
totalValue: {
metricOption: "AGGREGATED_TOTAL",
timeDimension: {
timeRange: {
startTime: "2016-01-01T00:00:00Z",
endTime: "2016-01-03T00:00:00Z"
}
},
value: "0"
},
dimensionalValues: [
{
metricOption: "AGGREGATED_DAILY",
timeDimension: {
timeRange: {
startTime: "2016-01-01T23:00:00Z"
}
},
value: "2"
},
{
metricOption: "AGGREGATED_DAILY",
timeDimension: {
timeRange: {
startTime: "2016-01-02T23:00:00Z"
}
},
value: "1"
}
]
}
}
] }
我想获得的,其中字段metricValues.dimensionalValues.value
被metricValues.dimensionalValues.timeDimension.timeRange.startTime
分组,然后由metricValues.metric
输出文档。
预期JSON输出:
[{
"startTime": "2016-01-01T00:00:00Z",
"metrics": {
"QUERIES_DIRECT": {
"value": 0
},
"QUERIES_INDIRECT": {
"value": 1
}
}
},
{
"startTime": "2016-01-02T00:00:00Z",
"metrics": {
"QUERIES_DIRECT": {
"value": 2
},
"QUERIES_INDIRECT": {
"value": 1
}
}
}]