Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda