使用聚合基于日期字段将MongoDB文档划分为多个文档

ou6hu8tu  于 2023-01-16  发布在  Go
关注(0)|答案(1)|浏览(124)

我在MongoDB上有一个包含飞行数据记录的文档。这些数据可以追溯到4年前,并且混在下面示例中显示的flight_records数组中。我想根据记录的年份将此文档拆分为单独的文档。我的示例文档如下-

{
"flight_number": "AS6312",
"airlines_code": "AS",
"flight_records": [{
        "status": "Landed 13:35",
        "origin": "ONT",
        "destination": "SEA",
        "date_of_journey": "2023-12-21",
        "scheduled": {
            "dep": 1671648300,
            "arr": 1671658500
        },
        "real": {
            "dep": 1671649420,
            "arr": 1671658520
        }
    },
    {
        "status": "Canceled",
        "origin": "ONT",
        "destination": "SEA",
        "date_of_journey": "2022-12-20",
        "scheduled": {
            "dep": 1671561900,
            "arr": 1671572100
        },
        "real": {
            "dep": 0,
            "arr": 0
        }
    },
    {
        "status": "Landed 13:09",
        "origin": "ONT",
        "destination": "SEA",
        "date_of_journey": "2021-12-19",
        "scheduled": {
            "dep": 1671475500,
            "arr": 1671485700
        },
        "real": {
            "dep": 1671475903,
            "arr": 1671484184
        }
    }
]

}
预期产出为三份文件,每份文件的期限为2021-2023年。

{
"flight_number": "AS6312",
"airlines_code": "AS",
"flight_records": [
  {
    "status": "Landed 13:35",
    "origin": "ONT",
    "destination": "SEA",
    "date_of_journey": "2023-12-21",
    "scheduled": {
      "dep": 1671648300,
      "arr": 1671658500
    },
    "real": {
      "dep": 1671649420,
      "arr": 1671658520
    }
  }]

}
同样,预期输出文档2-〉等...

{
"flight_number": "AS6312",
"airlines_code": "AS",
"flight_records": [
  {
    "status": "Canceled",
    "origin": "ONT",
    "destination": "SEA",
    "date_of_journey": "2022-12-20",
    "scheduled": {
      "dep": 1671561900,
      "arr": 1671572100
    },
    "real": {
      "dep": 0,
      "arr": 0
    }
  }]}

这些文档中的每一个都将根据记录的年份保存到单独的集合中。我尝试使用$match和$find,但无法获得正确的输出。

laik7k3q

laik7k3q1#

这是我临时想出的解决办法,希望对大家有用。

db.collection.aggregate(
{
"$unwind": "$flight_records"}, 
{
"$group": {
    _id: {
        $year: {
            $toDate: "$flight_records.date_of_journey"
        }
    },
    recordsForYear: {
        $push: "$flight_records"
    }
}})

结果如下:

{
"_id": 2021,
"recordsForYear": [
  {
    "date_of_journey": "2021-11-18",
    "destination": "DTW",
    "origin": "DCA",
    "real": {
      "arr": 1.668740944e+09,
      "dep": 1.668736446e+09
    },
    "scheduled": {
      "arr": 1.66874124e+09,
      "dep": 1.66873524e+09
    },
    "status": "Landed 22:09"
  },
  {
    "date_of_journey": "2021-11-16",
    "destination": "DCA",
    "origin": "DTW",
    "real": {
      "arr": 1.668644421e+09,
      "dep": 1.668640813e+09
    },
    "scheduled": {
      "arr": 1.66864524e+09,
      "dep": 1.6686396e+09
    },
    "status": "Landed 19:20"
  },
  {
    "date_of_journey": "2021-11-14",
    "destination": "DCA",
    "origin": "DTW",
    "real": {
      "arr": 1.668471887e+09,
      "dep": 1.66846811e+09
    },
    "scheduled": {
      "arr": 1.66847244e+09,
      "dep": 1.6684668e+09
    },
    "status": "Landed 19:24"
  }
]},{
"_id": 2023,
"recordsForYear": [
  {
    "date_of_journey": "2023-11-17",
    "destination": "DTW",
    "origin": "DCA",
    "real": {
      "arr": 1.668653594e+09,
      "dep": 1.668649558e+09
    },
    "scheduled": {
      "arr": 1.66865484e+09,
      "dep": 1.66864884e+09
    },
    "status": "Landed 21:53"
  },
  {
    "date_of_journey": "2023-11-15",
    "destination": "DCA",
    "origin": "DTW",
    "real": {
      "arr": 1.668557859e+09,
      "dep": 1.668553656e+09
    },
    "scheduled": {
      "arr": 1.66855884e+09,
      "dep": 1.6685532e+09
    },
    "status": "Landed 19:17"
  },
  {
    "date_of_journey": "2023-11-14",
    "destination": "DTW",
    "origin": "DCA",
    "real": {
      "arr": 1.668395275e+09,
      "dep": 1.668391115e+09
    },
    "scheduled": {
      "arr": 1.66839564e+09,
      "dep": 1.66838964e+09
    },
    "status": "Landed 22:07"
  }
]},{
"_id": 2022,
"recordsForYear": [
  {
    "date_of_journey": "2022-11-17",
    "destination": "DCA",
    "origin": "DTW",
    "real": {
      "arr": 1.668730907e+09,
      "dep": 1.668727074e+09
    },
    "scheduled": {
      "arr": 1.66873164e+09,
      "dep": 1.668726e+09
    },
    "status": "Landed 19:21"
  },
  {
    "date_of_journey": "2022-11-16",
    "destination": "DTW",
    "origin": "DCA",
    "real": {
      "arr": 1.668568424e+09,
      "dep": 1.668564614e+09
    },
    "scheduled": {
      "arr": 1.66856844e+09,
      "dep": 1.66856244e+09
    },
    "status": "Landed 22:13"
  },
  {
    "date_of_journey": "2022-11-15",
    "destination": "DTW",
    "origin": "DCA",
    "real": {
      "arr": 1.668481169e+09,
      "dep": 1.668477413e+09
    },
    "scheduled": {
      "arr": 1.66848204e+09,
      "dep": 1.66847604e+09
    },
    "status": "Landed 21:59"
  }
]

} ]

相关问题