Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda