Question about referencing information in another DB

Hello! I have a model in my code that includes the following:

ship: {
      type: mongoose.Schema.ObjectId,
      ref: 'Ship',
      required: [true, 'Review must belong to a ship.'],

This attaches a “Ship ID” to individual reviews. While nice, I’m wondering if I can attach a specific field in the Ship model called “shipName”. Is there a way for me to do this? I wasn’t sure if I would use a different “type” in the example code. Any help would be appreciated. Thank you.

I was able to accomplish the desired result with this code:

reviewSchema.pre(/^find/, function (next) {
  this.populate({
    path: 'ship',
    select: 'shipName',
  });
  next();
});

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.