Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Message schema that has multiple refs in mongodb using mongoose

I am getting stuck here because I'm not sure how to set multiple ref's to an object to specify the objectid of it.

For example: (how can I specify the sender to be a Seller or it can be a User, also, what would be the best approach if there is for this situation)

const messageSchema = mongoose.Schema({
  message: {
    text: { type: String, required: true },
    type: String, //type can be or 'message', 'image',
  },
  members: {
    seller: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Seller",
      required: true,
    },
    client: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
  },
  sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true,
  },
  readOn: { type: Date },
  read: Boolean, //}, {
  timestamps: true,
});

I want to be able to say:

sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: ["User", "Seller"],
    required: true,
  },

would this be possible? Thanks a lot in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can use refPath for dynamic referencing.

const messageSchema = mongoose.Schema({
  
 sender_type: String  // user or seller

 sender: {
    type: Schema.Types.ObjectId,
    refPath: 'sender_type'
  }
})

OR

you can use virtuals

const messageSchema = new mongoose.Schema({
...
 sender: {
    type: mongoose.Schema.Types.ObjectId,
    required: true
  },
...
},
{
    toJSON: { virtuals: true }
});

Then define virtual schemas like this:

order_schema.virtual('User', {
    ref: 'User', // Your User model name
    localField: 'sender', 
    foreignField: '_id', 
    justOne: true
});

order_schema.virtual('Seller', {
    ref: 'Seller',
    localField: 'sender',
    foreignField: '_id',
    justOne: true
});

Then in the database operations define two populates:

Model.find(search_filter)
      .populate('User')
      .populate('Seller')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!