title: Mongoose populate from references in another collection
body: <p>I feel like this has to have been asked before and yet, I can't seem to find an answer. I have the following Mongoose models:</p>
  
  <p><strong>Album Model</strong></p>
  
  <pre><code>const AlbumSchema = new Schema({
    name: {
      type: String,
      required: true,
      trim: true
    },
    user: {
      type: ObjectId,
      ref: 'User',
      required: false
    },
    photosCount: {
      type: Number,
      default: 0
    },
    videosCount: {
      type: Number,
      default: 0
    }
  })
  </code></pre>
  
  <p><strong>Media Model</strong></p>
  
  <pre><code>const MediaSchema = new Schema({
    type: {
      type: String,
      enum: ['image', 'video'],
      required: true
    },
    user: {
      type: ObjectId,
      ref: 'User',
      required: false
    },
    url: {
      type: String,
      required: true,
      trim: true
    },
    width: {
      type: Number,
      required: true
    },
    height: {
      type: Number,
      required: true
    },
    albums: [{
      type: ObjectId,
      ref: 'Album'
    }]
  })
  </code></pre>
  
  <p>Whenever an album is fetched, I would like there to be a <code>thumbnail</code> property on the album which holds the most recently added <code>media</code> object. </p>
  
  <p>I don't want to add a set of pointers to Media on the Album model because an album can potentially have tens of thousands of media. It makes sense to me that the media should hold references to the albums it's in and not the other way around.</p>
  
  <p>From the Mongoose docs it says:</p>
  
  <blockquote>
    <p>It is debatable that we really want two sets of pointers as they may
    get out of sync. Instead we could skip populating and directly find()
    the stories we are interested in.</p>
  </blockquote>
  
  <pre><code>Story
  .find({ _creator: aaron._id })
  .exec(function (err, stories) {
    if (err) return handleError(err);
    console.log('The stories are an array: ', stories);
  })
  </code></pre>
  
  <p>Not exactly sure how to apply that in this particular context or if it would even make sense to do so. I feel like that would be a bit ugly inside of a model but I'm new to Mongoose and MongoDb in general so I'm not sure what the best practice is to handle a scenario like this.</p>
  
  <p>To reiterate, I want a way to get the latest media for an arbitrary album and I do not want to have to store references in both collections if it can be avoided (for the reason I outlined above). I want the most recently added media object added to a given album to reside within a <code>thumbnail</code> property of that album, weather it's a single album or an array of albums.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I thought about adding this to the AlbumSchema:</p>
  
  <pre><code>thumbnail: {
    type: ObjectId,
    ref: 'Media'
  }
  </code></pre>
  
  <p>and then updating with a post save hook in my media model:</p>
  
  <pre><code>MediaSchema.post('save', function (media, next) {
    if (!media.isNew || !media.album) {
      return next()
    }
  
    const Album = mongoose.model('Album')
  
    Album.update({ _id: media.album }, { thumbnail: media }, next)
  })
  </code></pre>
  
  <p>this seems ugly though and if this media is later deleted, the system would have to find all albums with their thumbnail pointing to that media and update them to point to the next most recent media. </p>