Hook into bulkWrite #14263
Labels
enhancement
This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
new feature
This change adds new functionality, like a new method or class
Milestone
Prerequisites
🚀 Feature Proposal
Mongoose already supports middlewares for the majority of operations. But by reviewing the documentation and code I couldn't find a way to hook into
bulkWrite
.Which I found surprising, as
castBulkWrite
is called on eachbulkWrite
operation giving at least a perfect entry point forpre
modifyingbulkWrite
operations before they are executed:mongoose/lib/model.js
Line 3424 in ebf7d07
This allows mongoose internally for example to add the discriminator:
https://github.com/Automattic/mongoose/blob/master/lib/helpers/model/castBulkWrite.js#L70
Or
updatedAt
andcreatedAt
timestamps:https://github.com/Automattic/mongoose/blob/master/lib/helpers/model/castBulkWrite.js#L75
Motivation
I am already using mongoose discriminators to clearly seperate tenants within a single database.
Mongoose only allows setting a single
discriminatorKey
per model, so if I would like to "discriminate" additionally on another key I can't use discriminators for it.Now I want to shard data based on geolocation for which each query needs to include both the
discriminatorKey
and thelocation
. This is fairly easy to accomplish via hooks:But this doesn't touch
bulkWrite
operations. Here I would have to remember to include thelocation
within eachfilter
key. I would prefer if I could just hook into thecastBulkWrite
method instead similar to how mongoose is doing it internally with discriminators.The text was updated successfully, but these errors were encountered: