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

261
Vistas
MongoDB - how do I deep populate, an already populated field that has a reference to the schema it is in?

So I am having the following problem, I have a comments schema, which has a field called Replies and it is pointing toward the comment schema. The problem is that whenever I try to populate the following schema, everything except for the replies gets populated. Why is that happening, how do I fix it?

Comment Schema:

const {Schema, model} = require('mongoose')
const { ObjectId } = require('mongodb');

const commentSchema = new Schema({
    Author: 
    { 
        type: ObjectId, 
        required: true,
        ref: 'user'
    },
    Content: 
    { 
        type: String, 
        required: true 
    },    
    Likes:{
        type: [ObjectId],
        ref: 'user'
    },
    Replies: [this]
})

let comment = model('comment', commentSchema)
module.exports =  comment

And that is how I populate the posts model:

let allPosts = await postModel
    .find({})
    .populate('Author Comments')
    .populate(
        (
            {
                path: 'Comments', 
                populate:[
                    {path: 'Author'}
                ]
            },
            {
                path: 'Comments.Replies', 
                populate:[
                    {path: 'Author'}
                ]
            }
        )
    )

and this is my post model, referenced in the previous code sample:

const {Schema, model} = require('mongoose')
const { ObjectId } = require('mongodb');

const postSchema = new Schema({
    Author: 
    { 
        type: ObjectId, 
        required: true,
        ref: 'user'
    },
    Content: 
    { 
        type: String, 
        required: true 
    },
    Shares:{
        type: [ObjectId],
        ref: 'post'
    },
    Likes:{
        type: [ObjectId],
        ref: 'user'
    },
    Comments:{
        type: [ObjectId],
        ref: 'comment'
    }
})

let post = model('post', postSchema)
module.exports =  post
about 4 years ago · Juan Pablo Isaza
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