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

130
Vistas
How to populate mongoose with obejectId which is defined as Number?

I'm trying to populate my user requests.profileId but it returns only nulls.

I have the following schemas:

First Schema:

const profileSchema = new mongoose.Schema({
  _id: { type: Number }, //<- _id is defined as a number which represents mobile number (easier for me to handle)
  first: { type: String },
  second: { type: String },
});

module.exports = mongoose.model('Profile', profileSchema, 'profiles');

Second Schema:

const userSchema = new mongoose.Schema({
  firstName: { type: String },
  lastName: { type: String },
  requests: [
    {
      profileId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Profile',
      },
      requestTime: { type: Date, default: Date.now },
    },
  ],
});

module.exports = mongoose.model('User', userSchema, 'users');

Here is my code:

const user = await User.findById(req.user).populate('requests.profileId');
console.log(user.requests);

Here is the output:

[
  {
    _id: 6201633869648e2b74c00a10,
    profileId: null,
    requestTime: 2022-02-07T18:21:44.722Z
  },
  {
    _id: 6201633b69648e2b74c00a11,
    profileId: null,
    requestTime: 2022-02-07T18:21:47.238Z
  },
  {
    _id: 620238f9d2b5dd3dee6c41a2,
    profileId: null,
    requestTime: 2022-02-08T09:33:45.176Z
  },
  {
    _id: 620239253220343dfd7cfdd9,
    profileId: null,
    requestTime: 2022-02-08T09:34:29.780Z
  }
]

Here is the output without populate:

[
  {
    _id: 6201633869648e2b74c00a10,
    profileId: 393732353235303134343330, //<- typeof profileId is obeject
    requestTime: 2022-02-07T18:21:44.722Z
  },
  {
    _id: 6201633b69648e2b74c00a11,
    profileId: 393732353435353333313131,
    requestTime: 2022-02-07T18:21:47.238Z
  },
  {
    _id: 620238f9d2b5dd3dee6c41a2,
    profileId: 393732353435353333313131,
    requestTime: 2022-02-08T09:33:45.176Z
  },
  {
    _id: 620239253220343dfd7cfdd9,
    profileId: 393732353435353333313131,
    requestTime: 2022-02-08T09:34:29.780Z
  }
]

Currently Profile.findById(mobileNumber) works fine.

Any ideas what went wrong?

Will greatly appreciate your assistance.

Thanks in advance :)

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

0

Try this might work let me know if it doesn't

const user = await User.findById(req.user).populate('Profile');
console.log(user.requests); 
about 4 years ago · Juan Pablo Isaza Denunciar

0

try this:

User.findById(req.user).populate({
    path: 'requests',           
    populate: {
        path:  'profileId',
        model: 'Profile' 
    }
  })
about 4 years ago · Juan Pablo Isaza Denunciar

0

For future readers having the same issue!

I've found a solution for this issue.

I had to change the profileId type to Number in my userSchema:

const userSchema = new mongoose.Schema({
  firstName: { type: String },
  lastName: { type: String },
  requests: [
    {
      profileId: {
        type: Number // and NOT use type: mongoose.Schema.Types.ObjectId,
        ref: 'Profile',
      },
      requestTime: { type: Date, default: Date.now },
    },
  ],
});

module.exports = mongoose.model('User', userSchema, 'users');

Now it works!

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