*.remove( primary key )

Purpose

Used to remove records from the join table that is automatically generated during a many-to-many association. Unlike .add(), it only accepts the primary key of the model instance (defaults to record ID).

Overview

Parameters

Description Accepted Data Types Required ?
1 Primary Key string, int Yes

Example Usage

  1. User.find({name:'Mike'}).populate('pets').exec(function(e,r){
  2. r[0].pets.remove(7);
  3. r[0].save(console.log)
  4. });
  5. /*
  6. { pets:
  7. [ { name: 'Rainbow Dash',
  8. color: 'blue',
  9. id: 8,
  10. createdAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST),
  11. updatedAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST) },
  12. { name: 'Applejack',
  13. color: 'orange',
  14. id: 9,
  15. createdAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST),
  16. updatedAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST) } ],
  17. name: 'Mike',
  18. age: 16,
  19. createdAt: Wed Feb 12 2014 18:06:50 GMT-0600 (CST),
  20. updatedAt: Wed Feb 12 2014 19:30:54 GMT-0600 (CST),
  21. id: 7 }
  22. */

Notes

  • Any string arguments passed must be the primary key of the record.
  • .remove() alone won’t actually persist the change in associations to the databse. You should call .save() after using .add() or .remove().