title: how to get user object by userid from result of aggregation
body: <p>I'm trying to query mongoDB to fetch data aggregation. Here is my document:</p>
  
  <p><strong>Relations:</strong></p>
  
  <pre><code>{
      "_id" : 1,
      "from" : "a",
      "to" : "b",
      "message" : "a to b",
      "createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
  }
  {
      "_id" : 2,
      "from" : "a",
      "to" : "c",
      "message" : "a to c",
      "createdAt" : ISODate("2015-06-06T16:43:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:43:32.789Z")
  }
  {
      "_id" : 3,
      "from" : "b",
      "to" : "c",
      "message" : "b to c",
      "createdAt" : ISODate("2015-06-06T16:44:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:44:32.789Z")
  }
  {
      "_id" : 4,
      "from" : "a",
      "to" : "c",
      "message" : "a to c2",
      "createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
  }
  {
      "_id" : 5,
      "from" : "b",
      "to" : "c",
      "message" : "b to c2",
      "createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
  }
  </code></pre>
  
  <p><strong>User:</strong></p>
  
  <pre><code>{
      "_id" : 'a',
      "name" : "q",
      "createdAt" : ISODate("2015-06-14T17:20:27.288Z"),
      "updatedAt" : ISODate("2015-06-15T14:24:30.383Z")
  }
  {
      "_id" : 'b',
      "name" : "e",
      "createdAt" : ISODate("2015-06-14T17:20:29.288Z"),
      "updatedAt" : ISODate("2015-06-15T14:24:3.383Z")
  }
  {
      "_id" : 'c',
      "name" : "t",
      "createdAt" : ISODate("2015-06-14T17:20:28.288Z"),
      "updatedAt" : ISODate("2015-06-15T14:24:38.383Z")
  }
  </code></pre>
  
  <p>I've tried this:</p>
  
  <pre><code>db.collection.aggregate([
      {
          "$sort": {
              "updatedAt": -1
          }
      },
      {
          "$group": {
              "_id": {
                  "to": "$to",
                  "from": "$from"                
              },
              "id": { "$first": "$_id" },
              "message": { "$first": "$message" },
              "createdAt": { "$first": "$createdAt" },
              "updatedAt": { "$first": "$updatedAt" }
          }
      },
      {
          "$project": {
              "_id" : 0,
              "id": 1,
              "from" : "$_id.from",
              "to": "$_id.to",
              "message": 1,
              "createdAt": 1,
              "updatedAt": 1
          }
      }
  ]);
  </code></pre>
  
  <p>I've got this:</p>
  
  <pre><code>{
      "_id" : 1,
      "from" : "a",
      "to" : "b",
      "message" : "a to b",
      "createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
  }
  {
      "_id" : 4,
      "from" : "a",
      "to" : "c",
      "message" : "a to c2",
      "createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
  }
  {
      "_id" : 5,
      "from" : "b",
      "to" : "c",
      "message" : "b to c2",
      "createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
      "updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
  }
  </code></pre>
  
  <p>Now, i want to <strong>get one document with recent to-from combination</strong>. Example:</p>
  
  <pre><code>{
          "_id" : 1,
          "from" :{
              "_id" : 'a',
              "name" : "q",
              "createdAt" : ISODate("2015-06-14T17:20:27.288Z"),
              "updatedAt" : ISODate("2015-06-15T14:24:30.383Z")
          },
          "to" : {
              "_id" : 'b',
              "name" : "e",
              "createdAt" : ISODate("2015-06-14T17:20:29.288Z"),
              "updatedAt" : ISODate("2015-06-15T14:24:3.383Z")
          },
          "message" : "a to b",
          "createdAt" : ISODate("2015-06-06T16:42:32.789Z"),
          "updatedAt" : ISODate("2015-06-06T16:42:32.789Z")
  }
  {
          "_id" : 4,
          "from" :{
              "_id" : 'a',
              "name" : "q",
              "createdAt" : ISODate("2015-06-14T17:20:27.288Z"),
              "updatedAt" : ISODate("2015-06-15T14:24:30.383Z")
          },
          "to" : {
              "_id" : 'c',
              "name" : "t",
              "createdAt" : ISODate("2015-06-14T17:20:28.288Z"),
              "updatedAt" : ISODate("2015-06-15T14:24:38.383Z")
          },
          "message" : "a to c2",
          "createdAt" : ISODate("2015-06-06T16:45:32.789Z"),
          "updatedAt" : ISODate("2015-06-06T16:45:32.789Z")
  }
  {
          "_id" : 5,
          "from" : {
              "_id" : 'b',
              "name" : "e",
              "createdAt" : ISODate("2015-06-14T17:20:29.288Z"),
              "updatedAt" : ISODate("2015-06-15T14:24:3.383Z")
          },
          "to" : {
              "_id" : 'c',
              "name" : "t",
              "createdAt" : ISODate("2015-06-14T17:20:28.288Z"),
              "updatedAt" : ISODate("2015-06-15T14:24:38.383Z")
          },
          "message" : "b to c2",
          "createdAt" : ISODate("2015-06-06T16:46:32.789Z"),
          "updatedAt" : ISODate("2015-06-06T16:46:32.789Z")
  }
  </code></pre>
  
  <p>Any help with code is appreciated.</p>