I have the following sample collection Contract:
/* 1 */
{
    "_id" : "Contract_1",
    "ContactId" : "Contact_1",
    "Specifications" : [ 
        {
            "Description" : "Descrizione1",
            "VehicleId" : "Vehicle_1",
            "Customizations" : [ 
                {
                    "Description" : "Random furniture",
                    "ContactId" : "Contact_5"
                }, 
                {
                    "Description" : "Random furniture 2",
                    "ContactId" : "Contact_3"
                }
            ]
        }, 
        {
            "Description" : "Descrizione2",
            "VehicleId" : "Vehicle_2",
            "Customizations" : [ 
                {
                    "Description" : "Random furniture 3",
                    "ContactId" : "Contact_5"
                }, 
                {
                    "Description" : "Random furniture 4",
                    "ContactId" : "Contact_3"
                }
            ]
        }
    ]
}
/* 2 */
{
    "_id" : "Contract_2",
    "ContactId" : "Contact_2",
    "Specifications" : [ 
        {
            "Description" : "Descrizione1",
            "VehicleId" : "Vehicle_1",
            "Customizations" : [ 
                {
                    "Description" : "Random furniture",
                    "ContactId" : "Contact_5"
                }, 
                {
                    "Description" : "Random furniture 2",
                    "ContactId" : "Contact_3"
                }
            ]
        }, 
        {
            "Description" : "Descrizione2",
            "VehicleId" : "Vehicle_2",
            "Customizations" : [ 
                {
                    "Description" : "Random furniture",
                    "ContactId" : "Contact_5"
                }, 
                {
                    "Description" : "Random furniture 2",
                    "ContactId" : "Contact_3"
                }
            ]
        }
    ]
}
ContactId and VehicleId needs to be retrieved by lookup from their respective collections. To do so, I did the following query:
    db.getCollection('Contract').aggregate([
    {$lookup:
        { 
        from: "Contact",
        localField: "ContactId",
        foreignField: "_id",
        as: "Contact"
        }
     },
     {$unwind: "$Contact"},
     {$unwind: "$Specifications"},
     {$lookup:
        { 
        from: "Vehicle",
        localField: "Specifications.VehicleId",
        foreignField: "_id",
        as: "Specifications.Vehicle"
        }
     },
     {$unwind: "$Specifications.Vehicle"},
     {$unwind: "$Specifications.Customizations"},
     {$lookup:
        { 
        from: "Contact",
        localField: "Specifications.Customizations.ContactId",
        foreignField: "_id",
        as: "Specifications.Customizations.Contact"
        }
     },
     {$unwind: "$Specifications.Customizations.Contact"}
])
And I get something like this:
/* 1 */
{
    "_id" : "Contract_1",
    "ContactId" : "Contact_1",
    "Specifications" : {
        "Description" : "Descrizione1",
        "VehicleId" : "Vehicle_1",
        "Vehicle" : {
            "_id" : "Vehicle_1",
            "FrameNumber" : "asdasd33",
        },
        "Customizations" : {
            "Description" : "Random furniture",
            "ContactId" : "Contact_5",
            "Contact" : {
                "_id" : "Contact_5",
                "Name" : "Nome5"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_1",
        "Name" : "Nome"
    }
}
/* 2 */
{
    "_id" : "Contract_1",
    "ContactId" : "Contact_1",
    "Specifications" : {
        "Description" : "Descrizione1",
        "VehicleId" : "Vehicle_1",
        "Vehicle" : {
            "_id" : "Vehicle_1",
            "FrameNumber" : "asdasd33",
        },
        "Customizations" : {
            "Description" : "Random furniture 2",
            "ContactId" : "Contact_3",
            "Contact" : {
                "_id" : "Contact_3",
                "Name" : "Nome3"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_1",
        "Name" : "Nome"
    }
}
/* 3 */
{
    "_id" : "Contract_1",
    "ContactId" : "Contact_1",
    "Specifications" : {
        "Description" : "Descrizione2",
        "VehicleId" : "Vehicle_2",
        "Vehicle" : {
            "_id" : "Vehicle_2",
            "FrameNumber" : "frame2",
        },
        "Customizations" : {
            "Description" : "Random furniture 3",
            "ContactId" : "Contact_5",
            "Contact" : {
                "_id" : "Contact_5",
                "Name" : "Nome5"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_1",
        "Name" : "Nome"
    }
}
/* 4 */
{
    "_id" : "Contract_1",
    "ContactId" : "Contact_1",
    "Specifications" : {
        "Description" : "Descrizione2",
        "VehicleId" : "Vehicle_2",
        "Vehicle" : {
            "_id" : "Vehicle_2",
            "FrameNumber" : "frame2",
        },
        "Customizations" : {
            "Description" : "Random furniture 4",
            "ContactId" : "Contact_3",
            "Contact" : {
                "_id" : "Contact_3",
                "Name" : "Nome3"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_1",
        "Name" : "Nome"
    }
}
/* 5 */
{
    "_id" : "Contract_2",
    "ContactId" : "Contact_2",
    "Specifications" : {
        "Description" : "Descrizione1",
        "VehicleId" : "Vehicle_1",
        "Vehicle" : {
            "_id" : "Vehicle_1",
            "FrameNumber" : "asdasd33",
        },
        "Customizations" : {
            "Description" : "Random furniture",
            "ContactId" : "Contact_5",
            "Contact" : {
                "_id" : "Contact_5",
                "Name" : "Nome5"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_2",
        "Name" : "Nome"
    }
}
/* 6 */
{
    "_id" : "Contract_2",
    "ContactId" : "Contact_2",
    "Specifications" : {
        "Description" : "Descrizione1",
        "VehicleId" : "Vehicle_1",
        "Vehicle" : {
            "_id" : "Vehicle_1",
            "FrameNumber" : "asdasd33",
        },
        "Customizations" : {
            "Description" : "Random furniture 2",
            "ContactId" : "Contact_3",
            "Contact" : {
                "_id" : "Contact_3",
                "Name" : "Nome3"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_2",
        "Name" : "Nome"
    }
}
/* 7 */
{
    "_id" : "Contract_2",
    "ContactId" : "Contact_2",
    "Specifications" : {
        "Description" : "Descrizione2",
        "VehicleId" : "Vehicle_2",
        "Vehicle" : {
            "_id" : "Vehicle_2",
            "FrameNumber" : "frame2",
        },
        "Customizations" : {
            "Description" : "Random furniture",
            "ContactId" : "Contact_5",
            "Contact" : {
                "_id" : "Contact_5",
                "Name" : "Nome5"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_2",
        "Name" : "Nome"
    }
}
/* 8 */
{
    "_id" : "Contract_2",
    "ContactId" : "Contact_2",
    "Specifications" : {
        "Description" : "Descrizione2",
        "VehicleId" : "Vehicle_2",
        "Vehicle" : {
            "_id" : "Vehicle_2",
            "FrameNumber" : "frame2",
        },
        "Customizations" : {
            "Description" : "Random furniture 2",
            "ContactId" : "Contact_3",
            "Contact" : {
                "_id" : "Contact_3",
                "Name" : "Nome3"
            }
        }
    },
    "Contact" : {
        "_id" : "Contact_2",
        "Name" : "Nome"
    }
}
How can I group everything back to get 2 Contract as the starting point, but with all the informations that I get from the lookups? I'd like to group by the Contract _id field but the 2 nested arrays don't really help to do so.
Edit: ExpectedResult:
/* 1 */
{
    "_id" : "Contract_1",
    "ContactId" : "Contact_1",
    "Contact" : {
        "_id" : "Contact_1",
        "Name" : "Nome"
    },
    "Specifications" : [ 
        {
            "Description" : "Descrizione1",
            "VehicleId" : "Vehicle_1",
            "Vehicle" : {
                "_id" : "Vehicle_1",
                "FrameNumber" : "asdasd33"
            },
            "Customizations" : [ 
                {
                    "Description" : "Random furniture",
                    "ContactId" : "Contact_5",
                    "Contact" : {
                        "_id" : "Contact_5",
                        "Name" : "Nome5"
                    }
                }, 
                {
                    "Description" : "Random furniture 2",
                    "ContactId" : "Contact_3",
                    "Contact" : {
                        "_id" : "Contact_3",
                        "Name" : "Nome3"
                    }
                }
            ]
        }, 
        {
            "Description" : "Descrizione2",
            "VehicleId" : "Vehicle_2",
            "Vehicle" : {
                "_id" : "Vehicle_2",
                "FrameNumber" : "frame2"
            },
            "Customizations" : [ 
                {
                    "Description" : "Random furniture 3",
                    "ContactId" : "Contact_5",
                    "Contact" : {
                        "_id" : "Contact_5",
                        "Name" : "Nome5"
                    }
                }, 
                {
                    "Description" : "Random furniture 4",
                    "ContactId" : "Contact_3",
                    "Contact" : {
                        "_id" : "Contact_3",
                        "Name" : "Nome3"
                    }
                }
            ]
        }
    ]
}
/* 2 */
{
    "_id" : "Contract_2",
    "ContactId" : "Contact_2",
    "Contact" : {
        "_id" : "Contact_2",
        "Name" : "Nome2"
    }
    "Specifications" : [ 
        {
            "Description" : "Descrizione1",
            "VehicleId" : "Vehicle_1",
            "Vehicle" : {
                "_id" : "Vehicle_1",
                "FrameNumber" : "asdasd33"
            },
            "Customizations" : [ 
                {
                    "Description" : "Random furniture",
                    "ContactId" : "Contact_5",
                    "Contact" : {
                        "_id" : "Contact_5",
                        "Name" : "Nome5"
                    }
                }, 
                {
                    "Description" : "Random furniture 2",
                    "ContactId" : "Contact_3",
                    "Contact" : {
                        "_id" : "Contact_3",
                        "Name" : "Nome3"
                    }
                }
            ]
        }, 
        {
            "Description" : "Descrizione2",
            "VehicleId" : "Vehicle_2",
            "Vehicle" : {
                "_id" : "Vehicle_1",
                "FrameNumber" : "frame2"
            },
            "Customizations" : [ 
                {
                    "Description" : "Random furniture",
                    "ContactId" : "Contact_5",
                    "Contact" : {
                        "_id" : "Contact_5",
                        "Name" : "Nome5"
                    }
                }, 
                {
                    "Description" : "Random furniture 2",
                    "ContactId" : "Contact_3",
                    "Contact" : {
                        "_id" : "Contact_3",
                        "Name" : "Nome3"
                    }
                }
            ]
        }
    ]
}