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

173
Views
Cómo agregar referencia y datos en otro esquema en mongoose

Estoy creando un esquema Mongoose para una tienda

Tengo dos esquemas de owner y shop . Estoy tratando de agregar datos en show con ref como propietario, pero no sé cómo hacerlo.

aquí está mi estructura de esquema

 const mongoose = require("mongoose"); var shopSchema = Schema({ location : String, startDate : Date, endDate : Date }); var ownerSchema = Schema({ fname : String, lname : String, shopPlace : [{ type: Schema.Types.ObjectId, ref: 'Shop' }] }); var Shop = mongoose.model('Shop', shopSchema); var Owner = mongoose.model('Owner', ownerSchema);

así es como se ve mi esquema y estoy tratando de agregar detalles del propietario y la tienda, pero no sé cómo hacerlo si es un esquema único, puedo hacerlo fácilmente pero con ref no puedo

 const Owner = require("../models/ownerSchema"); const addTask = async (req, res) => { let newOwmer = new Owner(req.body); try { newOwner = await newOwner.save(); cosnole.log("Added Successfully"); } catch (err) { console.log(err); } };

Puedo agregar fácilmente pero no sé cómo agregar una tienda

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

0

tendrias que hacer algo asi

 const mongoose = require("mongoose"); var ownerSchema = Schema({ fname : String, lname : String, shopPlace : [{ type: Schema.Types.ObjectId, ref: 'Shop' }] }); var Owner = mongoose.model('Owner', ownerSchema); var shopSchema = Schema({ location : String, startDate : Date, endDate : Date, owner: Owner }); var Shop = mongoose.model('Shop', shopSchema);

Luego, cuando cree una tienda, primero cree un propietario y agregue el propietario en la creación de una tienda. tal vez así

 // Import your shop and owner models const fetchedOwner = await Owner.get('the-owner-id') const createShop = { location : 'London' startDate : 'some date' endDate : 'another date' owner: fetchedOwner } await Shop.create(createShop)
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!