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

328
Views
Cómo eliminar el esquema relacionado en mongodb usando el nodo js (mangoose)

Estoy tratando de eliminar mi segundo esquema que tiene referencia en el primer esquema

propietarioEsquema.js

 var ownerSchema = Schema({ ownerId : String, fname : String, lname : String, shopPlace : { type: Schema.Types.ObjectId, ref: 'Shop' } }); var Owner = mongoose.model('Owner', ownerSchema);

shopSchema.js

 var shopSchema = Schema({ _id : String, shopName : String, location : String, startDate : Date, endDate : Date }); var Shop = mongoose.model('Shop', shopSchema);

así que estoy tratando de eliminar mi segundo esquema pero solo elimina el primer esquema

 const Owner = require("../models/ownerSchema"); const Shop = require("../models/shopSchema"); const deleteOnwerById = async (req, res) => { const { id } = req.params; let deletedShop = await Shop.deleteOne({_id:shopPlace}); // I can't delete my shop data let deletedTask = await Owner.deleteOne({ ownerId: id }); } // I can delete owner but not shop detail };

pero no elimina mi esquema relacionado

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

0

Primero, elimine su Propietario usando findByIdAndRemove o findOneAndDelete . Estos métodos le permiten especificar uno o varios campos para devolver desde los documentos eliminados pasándole un objeto de options opcional.

 let deletedOwner = await Owner.findByIdAndRemove(ownerId, {projection : "shopPlace"});

Luego use el lugar de la tienda del propietario para eliminar el lugar de la tienda correspondiente:

 await Shop.deleteOne({_id:deletedOwner.shopPlace});
about 4 years ago · Juan Pablo Isaza Report

0

Primero debe obtener un _id de shopPlace de su propietario:

 const Owner = require("../models/ownerSchema"); const Shop = require("../models/shopSchema"); const deleteOnwerById = async (req, res) => { const { id } = req.params; const owner = await Owner.findOne({ ownerId: id }).populate('shopPlace'); let deletedShop = await Shop.deleteOne({ _id:owner.shopPlace._id }); let deletedTask = await Owner.deleteOne({ ownerId: id }); } };

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!