title: Remove duplicates based on a key and referenced Objects in Mongodb?
body: <p>I have MongoDB models of Actor and Movies. The Mongoose schema of both the models is as following :</p>
  
  <pre><code>var ActorsSchema = new Schema({
      id : {
          type : Number
      },
      known_for:[{
          type: Schema.Types.ObjectId,
          ref: 'Movie'
      }]
  })
  
  var MovieSchema = new Schema({
      genres: [{
          type: Schema.Types.ObjectId,
          ref: 'Genre'
      }],
      id: {
          type: Number
      }
  });
  </code></pre>
  
  <p><strong>known_for</strong> attribute in the actor model contains the reference to a list of movies in which that actor has starred.</p>
  
  <p>I want to delete duplicate Actor records which would be determined using the id fieled (not the _id). But what I also want to do is delete the movies referenced in the deleted actor's record in the known_for field to also be deleted and I want to do that from the Mongo interface as the number of records in these documents is very large and performing this function programmatically would be time inefficient.</p>
  
  <p>I have looked in to a related <a href="https://stackoverflow.com/questions/13190370/how-to-remove-duplicates-based-on-a-key-in-mongodb" title="question">question</a> but it does not apply to models who reference other models as there fields.</p>