mongodb - Mongodb分组并使用空数组推送
问题描述
当有group
一个数组可能是empty
. 集合可能是这样的:
{
"_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
. 这是我执行的查询:
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
时就会产生问题,因为对于第一个 whenpushing
$Specifications.Customizations
将创建一个内部包含空元素的数组。我想要的是 IfSpecifications
是一个空数组,将保持不变而不在里面添加一个空元素。
解决方案
这是我可以看到嵌套数组的$unwind
and的缺点之一。$group
要摆脱这种情况,您需要再添加一个阶段$addFields
来过滤掉空的嵌套数组。
将其添加到管道的末尾
{ "$addFields": {
"Specifications": {
"$filter": {
"input": "$Specifications",
"cond": { "$ne": ["$$this.Description", undefined] }
}
}
}}
推荐阅读
- r - RStudio 状态文件不存在
- node.js - 尝试在 Heroku 上安装 FFMPEG
- c# - 如何从 C# 在 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\ 下创建注册表项
- python - 使用子构造函数覆盖多态超类中的方法
- c# - 添加迁移时出错 - 对象引用未设置为对象的实例
- c# - 尽管类/runtimetpye 匹配,但分配失败
- docker - 使用默认管道在 gitlab.com 中构建失败
- javascript - Express 错误地呈现 HTML
- python - 如何制作类型检查器将识别为str的自定义注释?
- laravel - 如何优化 Laravel 的查询