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)
});
1
  • I think when you insert the record in Supplier thats time check GstDetails.StateId exist or not. if not exist then insert the object Commented Feb 21, 2020 at 11:57

1 Answer 1

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"}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
nevermind, found the answer here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.