Mongodb组并使用空数组进行推送 [英] Mongodb group and push with empty arrays
问题描述
当存在可能为empty
的数组时,我遇到了group
的问题.
该集合可能是这样的:
I'm having a problem with a group
when there is an array that could be empty
.
The collection could be like this:
{
"_id" : "Contract_1",
"ContactId" : "Contact_1",
"Specifications" : [ ]
}
{
"_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"
}
]
}
]
}
{
"_id" : "Contract_3",
"ContactId" : "Contact_25",
"Specifications" : [
{
"Description" : "Descrizione1",
"VehicleId" : "Vehicle_1",
"Customizations" : []
},
{
"Description" : "Descrizione2",
"VehicleId" : "Vehicle_2",
"Customizations" : []
}
]
}
如您所见,有时Specifications
可以为null,也可以为Customizations
.
这是我执行的查询:
As you can see, sometimes Specifications
can be null, and also Customizations
.
And this is the query that I execute:
db.getCollection("Contract").aggregate([
{ "$lookup": {
"from": "Contact",
"localField": "ContactId",
"foreignField": "_id",
"as": "Contact"
}},
{ "$unwind": {"path":"$Contact", "preserveNullAndEmptyArrays":true }},
{ "$unwind": { "path": "$Specifications", "preserveNullAndEmptyArrays":true }},
{ "$lookup": {
"from": "Vehicle",
"localField": "Specifications.VehicleId",
"foreignField": "_id",
"as": "Specifications.Vehicle"
}},
{ "$unwind": {"path": {"$Specifications.Vehicle","preserveNullAndEmptyArrays":true} },
{ "$unwind": {"path": {"$Specifications.Customizations","preserveNullAndEmptyArrays":true} },
{ "$lookup": {
"from": "Contact",
"localField": "Specifications.Customizations.ContactId",
"foreignField": "_id",
"as": "Specifications.Customizations.Contact"
}},
{ "$unwind": {"path": {"$Specifications.Customizations.Contact","preserveNullAndEmptyArrays":true} },
{ "$group": {
"_id": {
"_id": "$_id",
"Description": "$Specifications.Description"
},
"ContactId": { "$first": "$ContactId" },
"Contact": { "$first": "$Contact" },
"Specifications": {
"$push": "$Specifications.Customizations"
}
}},
{ "$group": {
"_id": "$_id._id",
"ContactId": { "$first": "$ContactId" },
"Contact": { "$first": "$Contact" },
"Specifications": {
"$push": {
"Description": "$_id.Description",
"Customizations": "$Specifications"
}
}
}}
])
}},
{ "$group": {
"_id": "$_id._id",
"ContactId": { "$first": "$ContactId" },
"Contact": { "$first": "$Contact" },
"Specifications": {
"$push": {
"Description": "$_id.Description",
"Customizations": "$Specifications"
}
}
}}
])
查询执行后,当执行2 $group
时会产生问题,因为对于第一个查询,当pushing
$Specifications.Customizations
会创建一个内部带有空元素的数组.我想要的是,如果Specifications
是一个空数组,它将保持原样,而无需在其中添加空元素.
Once the query execute, when it's doing the 2 $group
it creates a problem, since for the first one when pushing
$Specifications.Customizations
will create an array with an empty element inside. What I want is that If Specifications
is an empty array, will stay so without adding an empty element inside.
推荐答案
这是我可以看到的 $unwind
和
This is I can see one of the drawback of the $unwind
and $group
for the nested arrays. To get rid from this you need to add one more stage $addFields
to filter out the empty nested arrays.
将其添加到管道的结尾
{ "$addFields": {
"Specifications": {
"$filter": {
"input": "$Specifications",
"cond": { "$ne": ["$$this.Description", undefined] }
}
}
}}
这篇关于Mongodb组并使用空数组进行推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!