DAQ
DAQ

Reputation: 191

update associated collections in MEAN

i am building a MEAN stack web application, i have two associated collections categories and brands.

now, when i add a new brand and choose its category automatically the category id save in brand schema, but i don't know how to update (set to empty) this field (category-id in brand schema) when i delete the category from Category schema... hope I made my problem clear .

const mongoose = require('mongoose');

  const categorySchema = mongoose.Schema({
  categoryname: { type: String, required:true},
  categoryBrands: [{
     type: mongoose.Schema.Types.ObjectId,
     ref: "Brand",
     required: true }]
    });
 module.exports = mongoose.model('Category', categorySchema);

brands Collection schema:

const mongoose = require('mongoose');
const brandSchema = mongoose.Schema({
   brandName: { type: String, required: true },
   brandCategory: {
       type: mongoose.Schema.Types.ObjectId,
       ref: "Category",
       required: true
    }
  });
  module.exports = mongoose.model('Brand', brandSchema);

Upvotes: 0

Views: 48

Answers (1)

You have to reference the Categories Schema in your js file where you want to execute Schema queries then

Following are the ways on model object you can do any of the following to remove document(s):

yourModelObject.findOneAndRemove(conditions, options, callback)

yourModelObject.findByIdAndRemove(id, options, callback)

Also check this this it might help you

Upvotes: 1

Related Questions