title: Mongoose Query Help: Paginate, Sort, Limit on Nested Array
body: <p>I am have a chat Mongoose model in the below is the sample data.  If this is still not clear please revert back to me with your questions. Any help is greatly appreciated.</p>
  
  <pre><code>        { 
              "_id" : ObjectId("5745910831a1sd58d070a8faa"), 
              "messages" : [
                  {
                      "user" : "user1", 
                      "message" : "How are you user1?", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:36:00.468+0000"), 
                      "_id" : ObjectId("5745912c31a1c58d070a904d")
                  }, 
                  {
                      "user" : "user1", 
                      "message" : "Hello user1", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:38:53.893+0000"), 
                      "_id" : ObjectId("5745912531a1c58d070a902e")
                  }
              ], 
              "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
              "users" : [
                  "57450b4506561ff5052f0a66", 
                  "57450d8108d8d22c06cf138f"
              ], 
              "__v" : NumberInt(0)
          },
          { 
              "_id" : ObjectId("57458e9331a1c58d070a8e30"), 
              "messages" : [
                  {
                      "user" : "user2", 
                      "message" : "How are you user2", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:46:03.240+0000"), 
                      "_id" : ObjectId("574590f331a1c58d070a8ede")
                  }, 
                  {
                      "user" : "user2", 
                      "message" : "Hello user2", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:48:53.925+0000"), 
                      "_id" : ObjectId("574590e931a1c58d070a8eab")
                  }
              ], 
              "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
              "users" : [
                  "5745149e3aaab38706c00b64", 
                  "57450d8108d8d22c06cf138f"
              ], 
              "__v" : NumberInt(0)
          }
          { 
              "_id" : ObjectId("5745910831a1c58d070a8faa"), 
              "messages" : [
                  {
                      "user" : "user3", 
                      "message" : "How are you user3?", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:56:00.468+0000"), 
                      "_id" : ObjectId("5745912c31a1c58d070a904d")
                  }, 
                  {
                      "user" : "user3", 
                      "message" : "Hello user3", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:58:53.893+0000"), 
                      "_id" : ObjectId("5745912531a1c58d070a902e")
                  }
              ], 
              "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
              "users" : [
                  "57450b4506561ff5052f0a66", 
                  "57450d8108d8d22c06cf138f"
              ], 
              "__v" : NumberInt(0)
          },
          { 
              "_id" : ObjectId("5745910831a1c58d070a8faa"), 
              "messages" : [
                  {
                      "user" : "user4", 
                      "message" : "How are you user4?", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:66:00.468+0000"), 
                      "_id" : ObjectId("5745912c31a1c58d070a904d")
                  }, 
                  {
                      "user" : "user4", 
                      "message" : "Hello user4", 
                      "readInd" : "N", 
                      "createDate" : ISODate("2016-05-25T11:68:53.893+0000"), 
                      "_id" : ObjectId("5745912531a1c58d070a902e")
                  }
              ], 
              "createDate" : ISODate("2016-05-25T11:35:20.534+0000"), 
              "users" : [
                  "57450b4506561ff5052f0a66", 
                  "57450d8108d8d22c06cf138f"
              ], 
              "__v" : NumberInt(0)
          }
  </code></pre>
  
  <p>below is the explanation:</p>
  
  <ol>
  <li>user1 sent 2 messages at 11:36 and 11:38 respectively </li>
  <li>user2 sent 2 messages at 11:46 and 11:48 respectively </li>
  <li>user3 sent 2 messages at 11:56 and 11:58 respectively</li>
  <li>user4 sent 2 messages at 11:66 and 11:68 respectively</li>
  </ol>
  
  <p>My Expected Result is: </p>
  
  <p><strong>Pagination/limit Criteria:</strong> </p>
  
  <ol>
  <li>show 2 records per page. </li>
  <li>show only the Most recent message based on user.</li>
  </ol>
  
  <p><strong>Sample output:</strong></p>
  
  <p><strong>Page1:</strong></p>
  
  <pre><code>   "user" : "57450d8108d8d22c06cf138f",              
     "message" : "How are you user4?"
  
     "user" : "57450d8108d8d22c06cf138f", 
     "message" : "How are you user3?"
  </code></pre>
  
  <p><strong>Page2:</strong></p>
  
  <pre><code>   "user" : "57450d8108d8d22c06cf138f", 
     "message" : "How are you user2"
  
     "user" : "57450d8108d8d22c06cf138f", 
     "message" : "How are you user1?"
  </code></pre>