1

I am trying to delete the duplicate object inside the array in multiple documents in Mongodb. I try many ways but not able to fix

Document Structure:-

{ 
  "_id" : ObjectId("5a544fe234602415114601d3"), 
  "GstDetails" : [
      {
          "_id" : ObjectId("5e4837374d62f4c95163908e"), 
          "StateId" : "1", 
          "GstIn" : "33ABFFM1655H1ZF", 
          "StateDesc" : "TAMIL NADU", 
          "CityDesc" : "CHENNAI"
      }, 
      {
          "_id" : ObjectId("5e4837484d62f4c9516395e8"), 
          "StateId" : "1", 
          "GstIn" : "33ABFFM1655H1ZF", 
          "StateDesc" : "TAMIL NADU", 
          "CityDesc" : "CHENNAI"
      }
  ]
}

Like that many more documents I tried:-

  db.Supplier.find({ "GstDetails": { $size: 2 } }).limit(1).forEach(function (doc) {
  var stateId;
  doc.GstDetails.forEach(function (data) {
    if (data.StateId == stateId) {
        pull doc.GstDetails[0];
    } else {
      stateId = data.StateId
    }
    print(JSON.stringify(doc));
  });
  db.Supplier.save(doc)
});
Mayur Vaghasiya
  • 1,383
  • 2
  • 12
  • 24
  • I think when you insert the record in `Supplier` thats time check `GstDetails.StateId` exist or not. if not exist then insert the object – Mayur Vaghasiya Feb 21 '20 at 11:57

1 Answers1

1

Check if aggregation below meets your requirements:

db.Supplier.aggregate([
  {
    $unwind: "$GstDetails"
  },
  {
    $group: {
      _id: {
        _id: "$_id",
        StateId: "$GstDetails.StateId"
      },
      GstDetails: {
        $push: "$GstDetails"
      }
    }
  },
  {
    $addFields: {
      GstDetails: {
        $slice: [
          "$GstDetails",
          1
        ]
      }
    }
  },
  {
    $unwind: "$GstDetails"
  },
  {
    $group: {
      _id: "$_id._id",
      GstDetails: {
        $push: "$GstDetails"
      }
    }
  }
])

MongoPlayground

Note: This read-only query. If it is OK, you need to add as last stage below operator (once you execute it, it will update your documents, no rollback available):

{$out: "Supplier"}
Valijon
  • 12,667
  • 4
  • 34
  • 67
  • does the $out stage lock the whole collection while the documents are being replaced? or is the replacement done one document at a time? also what would happen if the operation gets interrupted halfway? would it lead to data corruption? – Dĵ ΝιΓΞΗΛψΚ Feb 22 '20 at 04:27
  • nevermind, found the answer [here](https://docs.mongodb.com/manual/reference/operator/aggregation/out/index.html#replace-existing-collection) – Dĵ ΝιΓΞΗΛψΚ Feb 22 '20 at 04:30