title: How to find documents with unique records in MongoDB?
body: <p>I have a collection with several documents in it of jobs to process using another system. I look up 5 objects from this table like this:</p>
  
  <pre><code>Work.find({status: 'new'})
    .sort({priority: 'desc'})
    .limit(5)
    .exec(function (err, work){})
  </code></pre>
  
  <p>There is another field on these documents which determines that only one job with a given unique value can be ran at the same time.</p>
  
  <p>For example these 3 jobs:</p>
  
  <pre><code>{uniqueVal: 1, priority: 1, type: "scrape", status:"new"}
  {uniqueVal: 1, priority: 1, type: "scrape", status:"new"}
  {uniqueVal: 2, priority: 1, type: "scrape", status:"new"}
  </code></pre>
  
  <p>There are 2 records with the uniqueVal of 1. Is there anything I can do to only pull one record with the value 1?</p>
  
  <p>Note: These values are not predetermined values like in the example, they are ObjectIds of other documents.</p>
  
  <p>I've looked into Distinct(), but it seems like it only returns the actual unique values, not the documents themselves.</p>