title: How do I introduce the equivalent of a foreign key into a MongoDB document?
body: <p>I'm building a project that has two collections: <code>cafes</code> to store information about various Cafes, and <code>reviews</code> to store information about cafe reviews, which is structured as follows:</p>
  <pre><code>Database
  |
  +--cafes (collection)
        +-- _id:5ffb7a6bf32d1b27ac8474d9
            cafeName:&quot;Customs Coffee
            photoURL:&quot;https://*******
  +--reviews (collection)
        +--_id:5ffb95b75624dd13d825ea5e
            userName:&quot;Josh&quot;
            stars:&quot;4&quot;
             title:&quot;Second review of customs coffee&quot;
            photo:&quot;photoURL.com&quot;
            blurb: &quot;this is the blurb for the second review of customs&quot;
            cafe:&quot;Customs Coffee&quot;
            createdAt:2021-01-11T00:03:03.842+00:00
  </code></pre>
  <p>I want each review document within the <code>reviews</code> collection to <strong>also</strong> contain the id of the cafe that the review is about- any suggestions on how I can do that? So for example, the review document as shown above would ideally look like this:</p>
  <pre><code>          id:5ffb95b75624dd13d825ea5e
            userName:&quot;Josh&quot;
            stars:&quot;4&quot;
             title:&quot;Second review of customs coffee&quot;
            photo:&quot;photoURL.com&quot;
            blurb: &quot;this is the blurb for the second review of customs&quot;
            cafe:&quot;Customs Coffee&quot;
            **cafeId:5ffb7a6bf32d1b27ac8474d9**
            createdAt:2021-01-11T00:03:03.842+00:00
  </code></pre>
  <p>Here's the Schema for the <code>cafes</code> and <code>reviews</code> collection:</p>
  <pre><code>const mongoose = require('mongoose')
  const Schema = mongoose.Schema
  
  const cafeSchema = new Schema({
      cafeName:String,
      photoURL:String,
  }, {timestamps:true})
  
  cafeSchema.virtual('reviews', {
      ref: 'Review',
      localField: '_id',
      foreignField: 'cafe'
  });
  
  const Cafe = mongoose.model('cafes', cafeSchema)
  
  module.exports = Cafe
  </code></pre>
  <pre><code>const mongoose = require('mongoose')
  const Schema = mongoose.Schema
  
  const reviewSchema = new Schema({
      userName:String,
      stars:String,
      title:String,
      photo:String,
      blurb:String,
      cafeName:String,
      cafe: {
          type: Schema.Types.ObjectId,
          ref: 'Cafe'
      }
  }, {timestamps:true})
  
  const Review = mongoose.model('reviews', reviewSchema)
  
  module.exports = Review
  </code></pre>