【发布时间】:2019-10-02 08:16:44
【问题描述】:
我有以下样本集合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 个嵌套数组并没有真正帮助这样做。
编辑:预期结果:
/* 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"
}
}
]
}
]
}
【问题讨论】:
-
能否提供联系人收集数据和预期结果
-
@AshwanthMadhav 我修复了查找,并添加了预期的结果。这是起始文档,包含从查找中检索到的所有信息
-
我需要联系人收集文件。它只是名称和 ID?
-
是的,还有Vehicle,只是为了更容易。真正的问题可以进一步简化。我需要倒带
{$unwind: "$Specifications"}, {$unwind: "$Specifications.Customizations"}部分。你可以想象一个前一个对象的聚合,只用这两条指令,然后回到前一个对象。查找并不重要
标签: mongodb group-by mongodb-query aggregation-framework