title: mongodb aggregation for finding recent data
body: <p>I have a model which looks like</p>
  
  <pre><code>{
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc41"),
  "refid" : 1,
  "item" : "xyz1",
  "qnid" : 1
  }
  {
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc42"),
  "refid" : 1,
  "item" : "xyz2",
  "qnid" : 2
  }
  {
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc43"),
  "refid" : 1,
  "item" : "xyz3",
  "qnid" : 3
  }
  {
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc44"),
  "refid" : 4,
  "item" : "xyz4",
  "qnid" : 4
  }
  {
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc45"),
  "refid" : 4,
  "item" : "xyz5",
  "qnid" : 5
  }
  </code></pre>
  
  <p>here refid is the reference id for the article and qnid is the actually id which holdes the version. i want to fetch all the articles ie refid with recent version data.</p>
  
  <p>i want to get the recent records aggregated by <strong>refid</strong></p>
  
  <p>article refid 1 has qnid 3 as the recent one. article refid 4 has qnid 5 as the recent one.
  ie </p>
  
  <pre><code>{
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc43"),
  "refid" : 1,
  "item" : "xyz3",
  "qnid" : 3
  }
  {
  "_id" : ObjectId("5b975b7ffb8361fa0cdacc45"),
  "refid" : 4,
  "item" : "xyz5",
  "qnid" : 5
  }
  </code></pre>
  
  <p>Please help me how can i do this.
  I used aggregate function with sort but it is not giving the data </p>