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

231
Views
Mongoose query to populate list values

How do I populate an index inside of a list? Say I have the following schema's

const supplierSchema = new mongoose.Schema({
    name: String,
    address: String,
    size: Number,
    ... (19 other fields)
    supplier_id: mongoose.Schema.Types.ObjectId,
});

const itemsSchema = new mongoose.Schema({
    name: String,
    date_time: {type: Date, default: Date.now},
    supplier_id: mongoose.Schema.Types.ObjectId,
    item_id: mongoose.Schema.Types.ObjectId,
});                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                 
const receiptSchema = new mongoose.Schema({
    date_time: {type: Date, default: Date.now},
    items: [itemSchema]
    receipt_id: mongoose.Schema.Types.ObjectId,
});
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
const Receipt = mongoose.model('Photo', photoSchema);

My Receipt is populated such that

Receipt.findById(<insertID>) currently gives:
{
    receipt_id: ObjectId("..."),
    date_time: ISODate("..."),
    items: [
      {
        name: "...",
        date_time: ISODate("..."),
        supplier_id: ObjectId("..."),
        item_id: ObjectId("...")
      },
      {
        name: "...",
        date_time: ISODate("..."),
        supplier_id: ObjectId("..."),
        item_id: ObjectId("...")
      }
    ],
  receipt_id: ObjectId("..."),
}

I would like for it to populate the supplier ID with name and address.

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

0

You can populate across multiple levels using populate:

Receipt
    .findById(<insertID>)
    .populate({
        path: 'items',
        populate: { path: 'supplier_id' }
    })
    .exec()

For more information see the official docs

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!