Second level nested Schema doesn’t populate

Sorry for the long code share. I just thought it will clarify things. I have two ways of pulling up orders. (1) rooting from Order schema. (2) rooting from User schema

I want to focus on the second way of pulling up orders. I’m trying to populate my User.find query like so: User > Order > Product

enter image description here

But this is all I’m getting in my post man tests. The product field doesn’t populate to my selected fields.
enter image description here

// User.js
const mongoose = require("mongoose");


const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: [true, "Email is required"]
  },
  password: {
    type: String,
    required: [true, "Password is required"]
  },
  isAdmin: {
    type: Boolean,
    default: false
  },
  orders: [
    {
      order: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Order'
      },

      _id: false
      
    }
  ]
});

module.exports = mongoose.model("User", userSchema);
// Order.js
const mongoose = require("mongoose");

const orderSchema = new mongoose.Schema({
  totalAmount: {
    type: Number,
    required: [true, "Total amount is required"]
  },
  purchasedOn: {
    type: Date,
    default: new Date()
  },
  buyer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  products: [
    {
      product: {
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'Product', 
        required: true
      },
      quantity: {
        type: Number,
        required: [true, "Product quantity is required."],
        min: [1, "Minimum product quantity is 1."]
      },
      purchasePrice: {
        type: Number,
        min: [0, "Price cannot be negative."]
      },
      
      _id: false
    }
  ]
});


module.exports = mongoose.model("Order", orderSchema);
// Product.js
const mongoose = require("mongoose");


const productSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, "Product name is required"]
  },
  sku: {
    type: String,
    required: [true, "Product SKU is required."]
  },
  description: {
    type: String,
    required: [true, "Product description is required"]
  },
  price: {
    type: Number,
    required: [true, "Product price is required"]
  },
  isActive: {
    type: Boolean,
    default: true
  },
  createdOn: {
    type: Date,
    default: new Date()
  }, 
  orders: [
    {
      order: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Order'
      },

      _id: false
    }
  ]
});

module.exports = mongoose.model( "Product" , productSchema );

Leave a Comment