我有这样的文件:
{
"many" : {},
"other" : {},
"fields" : {},
"phases" : [
{
"type" : 10,
"states" : [
{
"type" : 10,
"time" : ISODate("2018-04-25T13:06:42.990+02:00")
},
{
"type" : 20,
"time" : ISODate("2018-04-25T13:26:12.122+02:00")
},
{
"type" : 30,
"time" : ISODate("2018-04-25T13:26:30.124+02:00")
}
]
},
{
"type" : 20,
"states" : [
{
"type" : 10,
"time" : ISODate("2018-04-25T13:27:58.201+02:00")
}
]
}
]
}
在聚合中,我尝试将包括父级类型
的状态
扁平化,如下所示(期望输出):
"states" : [
{
"phase": 10,
"type" : 10,
"time" : ISODate("2018-04-25T13:06:42.990+02:00")
},
{
"phase": 10,
"type" : 20,
"time" : ISODate("2018-04-25T13:26:12.122+02:00")
},
{
"phase": 10,
"type" : 30,
"time" : ISODate("2018-04-25T13:26:30.124+02:00")
},
{
"phase": 20,
"type" : 10,
"time" : ISODate("2018-04-25T13:27:58.201+02:00")
}
]
我已经使用以下聚合完成了额外的states
字段,但没有phase
字段:
db.docs.aggregate([
{
$addFields: {
states: {
$reduce: {
input: "$phases",
initialValue: [],
in: { $concatArrays: ["$$value", "$$this.states"] }
}
}
}
}
])
我认为应该保留“许多其他领域”,因此我认为分组不是一个选择。
MongoDB版本是3.4。
我尝试了许多方法都没有结果。我想知道是否可能以及如何实现。