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

292
Vistas
How to fill multiple models with mongodb using "population"

I am trying to display all the data with the "populate" statement. But I only get one "populate" but when I put many it doesn't work. what I want to do is to bring me the data from the "User", "Customer" model as well.

This is my code

My Model:

import { model, Schema } from 'mongoose';

const RoomSchema = new Schema({
  Users: [
    {
      agentId: {
        type: Schema.Types.ObjectId,
        ref: "User",
        required: false,
      },
      customerId: {
        type: Schema.Types.ObjectId,
        ref: "Customer",
        required: false,
      },
      typeId: Number, // 1 - agent, 2 - client
    },
  ],
  Messages: [
    {
      agentId: {
        type: Schema.Types.ObjectId,
        ref: "User",
        required: false,
      },
      customerId: {
        type: Schema.Types.ObjectId,
        ref: "Customer",
        required: false,
      },
      message: {
        type: String,
        required: true,
      },
      date: Date,
      sender: Number,
    },
  ],
  FinishAt: Date,
  FinishBy: String,
  typeFinishBy: Number, // 1 - agent, 2 - client
}, {
    timestamps: true,
    versionKey: false
});

export default model('Room', RoomSchema);

this is the sentence I am using

import Room from "../models/Room.js";

async function getOnlyRoom(id) {

 const foundRoom = await Room.findById(id)
 .populate('Users.agentId')
 .populate('Users.customerId')
 .populate('Messages.agentId')
 .populate('Messages.customerId')
 .execPopulate();

  return foundRoom
}

Image of Json Postman

only works with one

foundRoom.populate('Users.customerId')

Works with only populate

this is the error image of error

Thank you very much for your help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I have found the solution to my problem I was researching the solution on the mongoose website and did it with a single populate.

 const populateRoom = await foundRoom
.populate([
  { path: 'Users.agentId', select: 'name' },
  { path: 'Users.customerId', select: 'name' },
  { path: 'Messages.agentId', select: 'name' },
  { path: 'Messages.customerId', select: 'name' }
])

return populateRoom;
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