title: MongoDB/Mongoose aggregate with lookup, nested arrays, sort
body: <p>So, these are my models </p>
  
  <pre><code>let restaurantSchema = mongoose.Schema({
      name: 'string',
      restId: 'string',
      saved: {type: 'date', default: Date.now()}
  })
  
  let userSchema = mongoose.Schema({
      firstName: {type: 'string', default: ''},
      lastName: {type: 'string', default: ''},
      username: {
          type: 'string',
          required: true,
          unique: true
      },
      beenTo: [restaurantSchema],
      toGoTo: [restaurantSchema]
  })
  </code></pre>
  
  <p>I want to make a feed: 'user A wants to go to restaurant X, user B wants to go to restaurant Y, user A has been to restaurant Z', etc, all sorted primarily by the date. so I've been trying to use aggregate to come up with documents that contain the username and restaurant name, as well as the timestamp, such that I could sort all the documents, regardless of username, with a {'saved': -1}, or whatever the timestamp becomes. </p>
  
  <p>Here's about what I've got so far</p>
  
  <pre><code>User.aggregate([
      {$match: {username: {$ne: username}}},
      {$lookup: {
        from: "restaurants",
        localField: "beenTo.saved",
        foreignField: "saved",
        as: "savRest"
      }},
      {$project: {'savRest.saved': 1, 'savRest.name': 1, 'username': 1}},
      {$group: {
        _id: null,
        sav: {$push: '$savRest.saved'}
      }}
    ])
  </code></pre>
  
  <p>Given the arrays, I expect i need to $unwind at some point, but I'm not sure where in the pipeline that goes...</p>