title: How to remove duplicates with a certain condition in mongodb?
body: <p>For example, I have the following documents in my collection:</p>
  
  <pre><code>{
      "_id" : "GuqXmAkkARqhBDqhy",
      "beatmapset_id" : "342537",
      "version" : "MX",
      "diff_approach" : "5",
      "artist" : "Yousei Teikoku",
      "title" : "Kokou no Sousei",
      "difficultyrating" : "3.5552737712860107"
  }
  {
      "_id" : "oHLT7KqsB7bztBGvu",
      "beatmapset_id" : "342537",
      "version" : "HD",
      "diff_approach" : "5",
      "artist" : "Yousei Teikoku",
      "title" : "Kokou no Sousei",
      "difficultyrating" : "2.7515676021575928"
  }
  {
      "_id" : "GbotZfrPEwW69FkGD",
      "beatmapset_id" : "342537",
      "version" : "NM",
      "diff_approach" : "5",
      "artist" : "Yousei Teikoku",
      "title" : "Kokou no Sousei",
      "difficultyrating" : "0"
  }
  </code></pre>
  
  <p>These documents have the same key <code>beatmapset_id</code>
  I want to delete all duplicates but leave the document with the most <code>difficultyrating</code>.</p>
  
  <p>I tried <code>db.collection.ensureIndex({beatmapset_id: 1}, {unique: true, dropDups: true})</code> but it leaves a random document and I want the condition above.</p>
  
  <p>How can I do that?</p>