title: How to remove object taking into account references in Mongoose Node.js?
body: <p>This is my MongoDB schema:</p>
  
  <pre><code>var partnerSchema = new mongoose.Schema({
      name: String,
      products: [
          {
              type: mongoose.Schema.Types.ObjectId,
              ref: 'Product'
          }]
  });
  
  var productSchema = new mongoose.Schema({
      name: String,
      campaign: [
          {
              type: mongoose.Schema.Types.ObjectId,
              ref: 'Campaign'
          }
      ]
  });
  
  var campaignSchema = new mongoose.Schema({
      name: String,
  });
  
  
  module.exports = {
      Partner: mongoose.model('Partner', partnerSchema),
      Product: mongoose.model('Product', productSchema),
      Campaign: mongoose.model('Campaign', campaignSchema)
  }
  </code></pre>
  
  <p>And I wondering how can I remove object from any document taking into account references (maybe should I use somehow populate from mongoose)? For example if I will remove <code>Product</code> then I assume that I will remove also ref ID in <code>Partner</code> and all <code>Campaigns</code> which belong to this <code>Product</code>.</p>
  
  <p>At the moment I removing in this way:</p>
  
  <pre><code>var campSchema = require('../model/camp-schema');
  
      router.post('/removeProduct', function (req, res) {
              campSchema.Product.findOneAndRemove({ _id: req.body.productId }, function (err, response) {
                  if (err) throw err;
                  res.json(response);
              });
          });
  </code></pre>
  
  <p>However in mongo still left references.</p>