How to remove related object from mongoose document array

Company collection:

{
  "_id": {
      "$oid": "594196a685d5c72f00517f64"
  },
  "name": "company",
  "reports": [
      {
          "$oid": "5942afb5314f0235d9a3a302"
      },
      {
          "$oid": "5942b1641b10b236075bcd89"
      }
  ],
}

      

I understand that I need to use update $ pull to remove an item from reports.

How do I create a query to achieve this?

I still have it,

 var Company = require('../models/company');
 Company.update(
   { _id: company},
   { $pull: {'reports': report._id} }
 )

      

Is there a way to delete reports from this company automatically if I delete a report?

For example, instead of using it update()

in a company after deleting a report, it would be nice if I could just do remove()

in the report and the company needs to know that it has been deleted. (I am coming from the rails, so this is annoying)

+3


source to share





All Articles