title: MongoDB: Trying to use aggregate $unwind to fetch embedded documents
body: <p>I have a MongoDB collection [Users] each with an Array of embedded documents [Photos].
  I would like to be able to have a page which lists recent photos, for example - the last 10 photos uploaded by any user.</p>
  
  <p><strong>Users</strong></p>
  
  <pre><code>[
      {
          _id: ObjectID
          name: "Matthew"
          Photos: [
              {
                  _id: ObjectID
                  url: "http://www.example.com/photo1.jpg"
                  created: Date("2013-02-01")
              },
              {
                  _id: ObjectID
                  url: "http://www.example.com/photo3.jpg"
                  created: Date("2013-02-03")
              }
          ]
      },
      {
          _id: ObjectID
          name: "Bob"
          Photos: [
              {
                  _id: ObjectID
                  url: "http://www.example.com/photo2.jpg"
                  created: Date("2013-02-02")
              },
              {
                  _id: ObjectID
                  url: "http://www.example.com/photo4.jpg"
                  created: Date("2013-02-04")
              }
          ]
      }
  ]
  </code></pre>
  
  <p>My first attempt at solving this was to find all Users where Photos is not null, and sort it by the created date:</p>
  
  <pre><code>User.find({images:{$ne:[]}}).sort({"images.created":-1})
  </code></pre>
  
  <p>Unfortunately, this doesn't work as I need it to. It sorts Users by the most recent images, but still returns all images, and there is no way to limit the number of images returned by the function.</p>
  
  <p>I started looking into aggregate, and it seems like it might be the solution I'm looking for, but I'm having trouble finding out how to get it to work.</p>
  
  <p>Ideally, the type of result I would like returned would be like this:</p>
  
  <pre><code>results: [
      {
          _id: ObjectID (from Photo)
          name: "Bob"
          url: "http://www.example.com/photo4.jpg"
          created: Date("2013-02-04")
      }
      {
          _id: ObjectID (from Photo)
          name: "Matthew"
          url: "http://www.example.com/photo3.jpg"
          created: Date("2013-02-03")
      }
  ]
  </code></pre>
  
  <p>Each result should be a Photo, and I should be able to limit the results to a specific amount, and skip results for paged viewing.</p>
  
  <p>Please let me know if you need any more information, and thank you for your responses.</p>