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

275
Views
Mongoose - Populating a nested array of objects not working

I have a collection called Orders that contains this schema:

const mongoose = require('mongoose');

const orderSchema = new mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    restaurant: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Restaurant',
        required: true
    },
    dishes: [
        {
            dish: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Dish'
            },
            amount: Number
        }
    ],
    price: {
        type: Number,
        required: true
    },
    comment: {
        type: String,
        required: false
    },
    status: {
        type: String,
        enum: ['PROCESSING', 'CANCELLED', 'COMPLETED', 'ERROR'],
        default: 'PROCESSING'
    },
    timestamp: {
        type: Date,
        default: Date.now
    }
})

module.exports = mongoose.model('Order', orderSchema);

Inside my router, I have this code:

let orders = await Order.find({restaurant: restaurantID, status:'PROCESSING'}).populate('dishes._id').exec()

Order.find does not throw an exception, but it isnt working either. I want the res.body to look like this:

            {
                "_id": "objectID",
                "user": "objectID",
                "restaurant": "objectID",
                "dishes": [
                    {
                        "amount": number,
                        "dish": {
                            //dish object
                        }
                    },
                    ...
                ],
                //other order properties
            },
            ...
        ]

But for some reason the dishes array looks like this:

        "dishes": [
            {
                "amount": 1,
                "_id": "6184e848e6d1974a0569783d"
            }
        ],

What am I doing wrong?

I know that if populate() worked the res.body dishes array would not have a property called 'dish' and instead have a property called _id that would contain the dish object, but this shouldnt be hard to change once populate() works.

EDIT:

I realised that my createOrder route could be part of the problem since it ignores my schema and uses an id property for the objectID instead of dish. The array I save to the DB contains a property called id for the id instead of dish, but shouldnt my schema throw an exception when i try to save something like this to my database?

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

0

At first glance, I think the problem might be that you have a syntax problem. Try

.populate('dishes').exec()

instead of

.populate('dishes._id').exec()
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!