Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda