Database
[{"formName":"my form","updatedAt":"2021-11-02T23:30:15.123Z"},{"formName":"my form","updatedAt":"2021-10-02T23:30:15.123Z"},{"formName":"my form","updatedAt":"2020-06-02T23:30:15.123Z"},{"formName":"my form","updatedAt":"2020-07-02T23:30:15.123Z"},{"formName":"another form","updatedAt":"2021-10-01T23:30:15.123Z"}]
Query
db.collection.aggregate([{"$set":{"date":{"$cond":["$updatedAt",{"$dateFromString":{"dateString":"$updatedAt"}},{"$dateFromString":{"dateString":"$lastUpdated"}}]},"updatedAt":"$$REMOVE","lastUpdated":"$$REMOVE"}},{"$group":{"_id":{"text":"$formName","year":{"$year":"$date"}},"children":{"$push":{"text":{"$month":"$date"}}}}},{"$group":{"_id":"$_id.text","children":{"$push":{"text":"$_id.year","children":"$children"}}}},{"$set":{"text":"$_id","_id":"$$REMOVE"}}])