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

154
Views
How to get the result of a monogdb populated items

I have a file model referencing a note model with the note referencing a question model.

A file can have many notes and different notes can have many questions, how can I query a file by its ID in MongoDB to return all the questions under all notes which are under a file.

File--->Notes--->Questions

const mongoose = require('mongoose');
const { Schema } = mongoose;

mongoose.Promise = global.Promise;

const fileSchema = new Schema(
    {
        title: {
            type: String,
            required: [true, 'Please add a title'],
            trim: true,
        },
        user: {
            type: mongoose.Schema.ObjectId,
            ref: 'User',
            required: true,
        },
        notes: [
            {
                type: mongoose.Schema.ObjectId,
                ref: 'Note',
            },
        ],
       
    },
    {
        timestamps: true,
        toJSON: { virtuals: true },
        toObject: { virtuals: true },
    },
);

module.exports = mongoose.model('File', fileSchema);


const mongoose = require('mongoose');
const { Schema } = mongoose;

mongoose.Promise = global.Promise;

const noteSchema = new Schema(
    {
        title: {
            type: String,
            required: [true, 'Please add a title'],
            trim: true,
        },
       
        user: {
            type: mongoose.Schema.ObjectId,
            ref: 'User',
            required: true,
        },
       
        questions: [
            {
                type: mongoose.Schema.ObjectId,
                ref: 'Question',
            },
        ],
      
    },
    {
        timestamps: true,
        toJSON: { virtuals: true },
        toObject: { virtuals: true },
    },
);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This looks similar to Mongoose's example of Populating across multiple levels

Say you have a user schema which keeps track of the user's friends.

const userSchema = new Schema({
 name: String,
 friends: [{ type: ObjectId, ref: 'User' }]
});

Populate lets you get a list of a user's friends, but what if you also wanted a user's friends of friends? Specify the populate option to tell mongoose to populate the friends array of all the user's friends:

User.
  findOne({ name: 'Val' }).
  populate({
    path: 'friends',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'friends' }
  });

In your case it may look like this:

File
  .findById(id)
  .populate({
    path: 'notes',
    populate: { path: 'questions' }
  });
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!