0

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.

Alexander R Johansen
  • 2,737
  • 3
  • 18
  • 23
  • 1
    You can `populate`, or you can use [`$lookup`](https://www.mongodb.com/docs/v5.0/reference/operator/aggregation/lookup/). These are 2 different methods, both will get the supplier's data into the Receipt. You can see [here](https://stackoverflow.com/questions/62557902/mongodb-lookup-vs-mongoose-populate). `$lookup` should be more efficient, while `populate` (mongoose) is considered more structured. – nimrod serok May 18 '22 at 05:30

1 Answers1

1

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

lpizzinidev
  • 12,741
  • 2
  • 10
  • 29