我在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,但无法获得正确的输出。
1条答案
按热度按时间laik7k3q1#
这是我临时想出的解决办法,希望对大家有用。
结果如下:
} ]