Mongoose pre hooks with insertMany model functions

Mongoose pre hooks with insertMany model functions

On my journey to explore MongoDB and Mongoose's powerful features over the years, I encountered something called pre and post hooks, few years back. They are simple yet powerful tools with which you can manipulate model instances, queries, perform validation before or after an intended database operation etc. You can define your custom middleware functions on the schema level itself using these hooks.

No alt text provided for this image

Here the classical example would be the use of pre-save hook to encrypt the password while the document is being saved or if the password has changed during an update operation.

If you have gone through the mongoose documentation explaining the middlewares you would know that there can be 4 types of middlewares depending on the type of document method you are latching it onto. For example, the save method middlewares are document middlewares. Means the value of this is dynamically set to the document. Which basically means this holds the document that is about to be saved or updated. However, keep in mind you can't use the fat arrow function syntax here to define the middleware function for such hooks. The middleware needs to be defined using the function expression. The reason being the lexical nature of this within arrow functions. It will resolve to undefined instead of being dynamically resolved based on the scope of the caller.

However, while we are using hooks on insertMany we are actually defining a Model middleware, means the value of this in the function refers to the mongoose model itself. In the context of MongoDB, insertMany is a language specific driver method and has a mongosh version too. It can perform multiple write operations in ordered manner (if ordered has been set to true). When defining the middleware function for the mongoose pre hook on insertMany, we need to pass two arguments, instead of only one, like we did in the case of save, the first one being the next callback argument which is used to pass control to the very next method in line and the second one, the array of documents that is supplied to the insertMany method for the purpose of insert or update. We can then operate on each of those documents in a loop and perform some action within the middleware method, the way we need. The following example shows the use of pre middleware on insertMany that modifies the supplied documents by encrypting the password for each user before they are saved in the User collection.

No alt text provided for this image

I would suggest to console out the value of this in the middleware while trying to use the save and insertMany and figure out the differences between the two. The key takeaway from this article would be the understanding, that the parameters and their values supplied to the middleware would change depending on the method, the middlware is being declared on. I would highly suggest that you read through the mongoose documentation for a better understanding of all the mongoose middleware types.

Thanks for the article. Your "the fat arrow function syntax" just saved me. I was wondering why I can't access the 'this' keyword. Appreciated brother.

This was a great article. I think that you could do either of the awaits. I believe that they are redundant.

To view or add a comment, sign in

More articles by Palash Chanda

Others also viewed

Explore content categories