You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given that the post remove hook isn't called when a document is deleted (which is what should be expected due to the implementation), I wonder what would be the best way to achieve the deletion of references upon removal of a document. For example, if there are two schemas, users and groups, which model a n to n relationship where each user has an array of references to groups. A group is then removed so its objectId should be removed from all the users group array.
By mongoose design, the implementation of remove in service.js does not fire the remove hook, which means I cannot do the following in group.model.js:
module.exports=function(app){constmongooseClient=app.get('mongooseClient');const{ Schema }=mongooseClient;constgroups=newSchema({name: {type: String,required: true},accessRights: [{resourceType: {type: String},resource: {type: Schema.Types.ObjectId,refPath: 'accessRights.resourceType'},actions: {type: [String],default: []}}]},{timestamps: true});groups.post('remove',function(doc){// Remove doc._id from groups array in users});returnmongooseClient.model('groups',groups);};
What would be the best method to enable this behaviour?
The text was updated successfully, but these errors were encountered:
As lean is set to true by default, I suppose you meant setting it to false. Either does not work. The "problem" is with mongoose middleware, which states
There is no query hook for remove(), only for documents. If you set a 'remove' hook, it will be fired when you call myDoc.remove(), not when you call MyModel.remove(). Note: The create() function fires save() hooks.
It is not a problem per se, but I wanted to keep this functionality as close as possible to mongoose / the model and I'm not sure if a service hook is the right place to do it.
Given that the post remove hook isn't called when a document is deleted (which is what should be expected due to the implementation), I wonder what would be the best way to achieve the deletion of references upon removal of a document. For example, if there are two schemas, users and groups, which model a n to n relationship where each user has an array of references to groups. A group is then removed so its objectId should be removed from all the users group array.
By mongoose design, the implementation of remove in service.js does not fire the remove hook, which means I cannot do the following in group.model.js:
What would be the best method to enable this behaviour?
The text was updated successfully, but these errors were encountered: