title: How to delete Duplicate objects inside array in multiple documents in mongodb?
body: <p>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</p>
  
  <p>Document Structure:-</p>
  
  <pre><code>{ 
    "_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"
        }
    ]
  }
  </code></pre>
  
  <p>Like that many more documents
  I tried:- </p>
  
  <pre><code>  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)
  });
  </code></pre>