删除嵌套组中的重复项

问题描述 投票:1回答:1

我必须删除与嵌套数组和JSON中的对象中的父元素具有相同LocateId的元素(我正在使用MongoDB):

{
 "mainLocation": {
                  "locateId": {"$numberInt": "111111"},
                  "LocateName": "Indonesia",
                  "subLocation": [{
                                "locateId": {"$numberLong": "2222222222"}, *//this the refference for Child location*
                                "LocateName": "Jakarta Pusat",
                                "childLocation": [{
                                                   "locateId": {"$numberLong": "2222222222"},
                                                   *//if the LocateId is same with Sublocation.LocateId will removed*
                                                   "LocateName": "Jakarta Pusat",
                                                 },{
                                                   "locateId": {"$numberLong": "3333333333"},
                                                   "LocateName": "Jakarta Barat",
                                                 }]
                                 },{
                                "locateId": {"$numberLong": "1234123412"},
                                "LocateName": "Bandung",
                                "childLocation": []
                                 }]
                 }
}

以及我期望的是:

{
 "mainLocation": {
                  "locateId": {"$numberInt": "111111"},
                  "LocateName": "Indonesia",
                  "subLocation": [{
                                "locateId": {"$numberLong": "2222222222"},
                                "LocateName": "Jakarta Pusat",
                                "childLocation": [{
                                                   "locateId": {"$numberLong": "3333333333"},
                                                   "LocateName": "Jakarta Barat",
                                                 }] *//the element with same Id has been removed*
                                 },{
                                "locateId": {"$numberLong": "1234123412"},
                                "LocateName": "Bandung",
                                "childLocation": []
                                 }]
                 }
}

而且我尝试了简单的功能,至少可以按照我期望的顺序显示

db.pages.aggregate(
    [{ 
            "$group" : {
                LocatediId: "$mainLocation.LocatediId",
                subLocation : {
                        "$group" : { 
                        LocatediId: "$mainLocation.subLocation.LocatediId",
                        Locatedname: "$mainLocation.subLocation.Locatedname"
                        }}
        }}
    ]);

所以我可以将结果导出到JSON文件。

mongodb mongoose aggregate robo3t studio3t
1个回答
1
投票

尝试这个:

db.collection.aggregate([
   {
      $set: {
         "mainLocation.subLocation": {
            $map: {
               input: "$mainLocation.subLocation",
               as: "subLocation",
               in: {
                  $mergeObjects: [
                     "$$subLocation",
                     {
                        childLocation: {
                           $filter: {
                              input: "$$subLocation.childLocation",
                              cond: { $ne: ["$$this.locateId", "$$subLocation.locateId"] }
                           }
                        }
                     }
                  ]
               }
            }
         }
      }
   }
])

Mongo playground

© www.soinside.com 2019 - 2024. All rights reserved.