Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!