У меня есть следующий образец коллекции 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
и VehicleId
необходимо получить lookup
из их соответствующих коллекций.Для этого я выполнил следующий запрос:
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"}
])
И я получил что-то вроде этого:
/* 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"
}
}
Как я могу сгруппировать все обратно, чтобы получить 2 Контракт в качестве отправной точки,но со всей информацией, которую я получаю из поисков?Я бы хотел сгруппировать по полю Contract
_id
, но 2 вложенных массива на самом деле не помогают.
Редактировать: 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"
}
}
]
}
]
}