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

269
Views
How do i reference 1 Mongoose Schema inside another as its property?

I am trying to make a store app and have created two mongoose schemas. Order.js and Product.js, I want to reference the product schema as a property of the order schema.

Order.js

const mongoose = require('mongoose');
const Product=require('../models/product.js');

const orderSchema = new mongoose.Schema(

    {
        date: {
             type:Date, 
             default: Date.now},
        customerName: String,
        customerAddress: String,
        creditCard: Number,
        products:[] //product model
   
    },
     { timestamps: true }
);

product.js

const mongoose = require('mongoose');

const productSchema = new mongoose.Schema(
    {
        name: String,
        price: Number,
        category: String
    },
    { timestamps: true }
);

const Product = mongoose.model('Product', productSchema);
module.exports= Product;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is my approach when it comes to nested schemas in mongoose : first you have to create an object of your Product schema , to avoid duplicating your Product model , you can create an object :

product

with your fields , then create your Product schema by simply const Product = new Schema(product); , then you can import the product object here in your order.js and declare your schema as follows :

import product;
const Order = new Schema({
......
......
order : {
   type : product , 
   ......
}
})
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!