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

99
Views
How to populate all the user fields which is inside an object inside of an array and another array in mongoose

here is my course schema

const CourseSchema = new Schema(
  {
    courseName: {
      type: String,
      required: true,
      lowercase: true,
    },
    comments: [
      [
        {
          user: {
            type: Schema.Types.ObjectId,
            ref: "Users",
            required: true,
          },
          comment: {
            type: String,
            required: true,
          },
          createdAt: {
            type: Date,
            required: true,
          },
        },
      ],
    ],
  },
  {
    timestamps: true,
  }
);
const Course = mongoose.model("Course", CourseSchema);

I want to populate the user field. I've tried many stack overflow solutions but none of them works for me.

I populated the model like this but, doing so it only populates the first index the of every model. courses = await Course.findOne({}).populate({ path: "comments.0.0.user", });

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can populate another level deeper, here's what you need to do:

 db.Course.findOne({}).populate({"path":"comments",populate:[{
    path:"user",
     model:"Users"
    }]})

Another way of nested populating data:

db.Course.findOne({}).populate("comments.user")
about 4 years ago · Juan Pablo Isaza Report
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!