1

I have to remove element that have same LocateId to parent element in nested array & object in JSON (I'm using 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": []
                                 }]
                 }
}

and what I expected will be :

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

and I tried simple funct, at least can show as sort as order that I expected

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

So I can export the result to JSON file.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gunawan
  • 11
  • 2

1 Answers1

1

Try this one:

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

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110