Mongodb group and push with empty arrays

醉酒当歌 提交于 2019-12-11 02:09:28

问题


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" : []
        }
    ]
}

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"
      }
    }
  }}
])

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.


回答1:


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.

Add this at the end of the pipeline

{ "$addFields": {
  "Specifications": {
    "$filter": {
      "input": "$Specifications",
      "cond": { "$ne": ["$$this.Description", undefined] }
    }
  }
}}


来源:https://stackoverflow.com/questions/56312636/mongodb-group-and-push-with-empty-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!