I have a document on MongoDB that contains flight data records. This data goes back 4 years , and is mixed up in the flight_records
array shown in the sample below. I want to split this document into separate documents based on the Year of the record. My Sample Document is as follows -
[
{
"flight_number": "DL1637",
"airlines_code": "DL",
"flight_records": [
{
"status": "Landed 22:09",
"origin": "DCA",
"destination": "DTW",
"date_of_journey": "2021-11-18",
"scheduled": {
"dep": 1668735240,
"arr": 1668741240
},
"real": {
"dep": 1668736446,
"arr": 1668740944
}
},
{
"status": "Landed 19:21",
"origin": "DTW",
"destination": "DCA",
"date_of_journey": "2022-11-17",
"scheduled": {
"dep": 1668726000,
"arr": 1668731640
},
"real": {
"dep": 1668727074,
"arr": 1668730907
}
},
{
"status": "Landed 21:53",
"origin": "DCA",
"destination": "DTW",
"date_of_journey": "2023-11-17",
"scheduled": {
"dep": 1668648840,
"arr": 1668654840
},
"real": {
"dep": 1668649558,
"arr": 1668653594
}
},
{
"status": "Landed 19:20",
"origin": "DTW",
"destination": "DCA",
"date_of_journey": "2021-11-16",
"scheduled": {
"dep": 1668639600,
"arr": 1668645240
},
"real": {
"dep": 1668640813,
"arr": 1668644421
}
},
{
"status": "Landed 22:13",
"origin": "DCA",
"destination": "DTW",
"date_of_journey": "2022-11-16",
"scheduled": {
"dep": 1668562440,
"arr": 1668568440
},
"real": {
"dep": 1668564614,
"arr": 1668568424
}
},
{
"status": "Landed 19:17",
"origin": "DTW",
"destination": "DCA",
"date_of_journey": "2023-11-15",
"scheduled": {
"dep": 1668553200,
"arr": 1668558840
},
"real": {
"dep": 1668553656,
"arr": 1668557859
}
},
{
"status": "Landed 21:59",
"origin": "DCA",
"destination": "DTW",
"date_of_journey": "2022-11-15",
"scheduled": {
"dep": 1668476040,
"arr": 1668482040
},
"real": {
"dep": 1668477413,
"arr": 1668481169
}
},
{
"status": "Landed 19:24",
"origin": "DTW",
"destination": "DCA",
"date_of_journey": "2021-11-14",
"scheduled": {
"dep": 1668466800,
"arr": 1668472440
},
"real": {
"dep": 1668468110,
"arr": 1668471887
}
},
{
"status": "Landed 22:07",
"origin": "DCA",
"destination": "DTW",
"date_of_journey": "2023-11-14",
"scheduled": {
"dep": 1668389640,
"arr": 1668395640
},
"real": {
"dep": 1668391115,
"arr": 1668395275
}
}
]
}
]
I am using the “$unwind”: “$flight_records” first to unwind the flight records array , and then I am thinking about grouping this data by Year of the record. But I am not able to get a proper result. Can someone guide me as to what proper aggregation tools I should be using here. I am thinking along these lines :
db.collection.aggregate({
"$unwind": "$flight_records",
"$group": {
"_id": "flight_records",
"date_of_journey": {
"year": {
"$year": new Date("$flight_records.date_of_journey")
}
}
}
})
But I am getting this error :
error in query:
fail to parse content of query: cannot parse date: "$flight_records.date_of_journey" [parsing time "$flight_records.date_of_journey" as "2006-01-02T15:04:05.999Z07:00": cannot parse "$flight_records.date_of_journey" as "2006", parsing time "$flight_records.date_of_journey" as "2006-01-02": cannot parse "$flight_records.date_of_journey" as "2006"]
I am not able to figure out what I am doing wrong , any help is appreciated!