title: MongoDB / Mongoose one-to-many relationship
body: <p>Working on a database structure for a new project, and trying to figure out how exactly one-to-many relationships in MongoDB / mongoose work. </p>
  
  <p>So considering a following simple structure :</p>
  
  <p>I have a project table/schema with multiple images linked to it:</p>
  
  <pre><code>const ProjectSchema = mongoose.Schema({
      _id: mongoose.Types.ObjectId, // Just the id,
      name: { type: String, required: true, trim: true,  maxLength: 60 },
      description: { type: String, required: false }, 
      images: [{ 
          type: Schema.Types.ObjectId, ref: 'Image'
      }]
  });
  </code></pre>
  
  <p>And then an image table/schema:</p>
  
  <pre><code>const ImageSchema = new Schema({
      _id: mongoose.Types.ObjectId, // Just the id,
      url: { type: String, unique: true, required: true }
      project: { 
          type: Schema.Types.ObjectId, ref: 'Project'
      }        
  });
  </code></pre>
  
  <p>I want one-to-many between images and projects, so I save an image with a project id:</p>
  
  <pre><code>const image = new Image(
          {
                  project: project._id,
                  _id: new mongoose.Types.ObjectId(),
                  url: 'https://someurl',
          });        
          await image.save();
  </code></pre>
  
  <p>If I then find this image, and populate the project field - it contains all the project's info nicely, BUT if I find this project, it doesn't have anything in the images array (referencing Image):</p>
  
  <pre><code>images: [{ 
          type: Schema.Types.ObjectId, ref: 'Image'
      }]
  </code></pre>
  
  <p>I thought that with ref you're creating the foreign key reference between Project and Image and then both Image should have the linked Project and Project should see the linked image in the array.
  Is that not the case or am I missing something here ?</p>