在MongoDB中重构文档

在MongoDB中重构文档,mongodb,Mongodb,我在mongoDB中有这样的文档结构 { 'identifier': 'testanalysis', 'uuid': 'abc', 'days': [{'day': 20190122, 'value': 1}, {'day': 20190123, 'value': 2}, ] }, { 'identifier': 'testanalysis', 'uuid': 'xyz', 'days': [{'day': 20190122, 'value': 5}, {'day': 20190123, 'va

我在mongoDB中有这样的文档结构

{
'identifier': 'testanalysis',
'uuid': 'abc',
'days': [{'day': 20190122, 'value': 1}, {'day': 20190123, 'value': 2}, ]
},
{
'identifier': 'testanalysis',
'uuid': 'xyz',
'days': [{'day': 20190122, 'value': 5}, {'day': 20190123, 'value': 10}, ]
}
我希望得到如下结果。它看起来像是按
day
分组,并将
uuid
value
组合成一个对象

{
    'day': 20190122,
    'result': [
        {'uuid': 'abc', 'value': 1},
        {'uuid': 'xyz', 'value': 5},
    ]
},
{
    'day': 20190123,
    'result': [
        {'uuid': 'abc', 'value': 2},
        {'uuid': 'xyz', 'value': 10},
    ]
}
一点点


也许你在寻找加重:和预测
db.collection.aggregate([
  { $unwind: '$days' },
  { $group: {
      _id: "$days.day",
      day: { $first: "$days.day" },
      result: {
        $push: { 'uuid': '$uuid', 'value': '$days.value' }
      }
    }
  }
])