Outlier Pattern & Database Triggers

Hey everyone - I’m looking for some thoughts on the extended reference and outlier data patterns, specifically whether or not triggers should be used.

For example, pretend I have a simple user document with an array books, that holds up to 100 book IDs. In the rare circumstance that a user has over 100 books the boolean hasOutlierBooks is set to true. The user document would look something like the following…

{
   _id: ObjectID
   books: [BookObjectID, BookObjectID, ... ],
   countBooks: 105
   hasOutlierBooks: true
   ...
}

A user can add or remove a book. As a result, my function will check the count of books as well as the hasOutlierBooks boolean. If the count of books is below 100, the process is straightforward, a book is added or removed from the books array and the count is adjusted, there is no interaction with the outlier collection.

Meanwhile, in the event that the count of books is over 100, different logic is used to update the main books array, save a book to the outlier books collection, etc. This really should not happen too often, but it is something I need to account for.

My question is, should my realm function hold the logic for updating the outlier collection or should I move it to a trigger (which could even be linked to an AWS EventBridge)? The trigger could use a matched expression to check whenever hasOutlierBooks is true or countBooks >= 100. I am not concerned that the realm function will time out.

My concern / question is really focused on efficiency of triggers as I intend my application to be able to scale. Would I have to index hasOutlierBooks and countBooks (which I do not want to do) for the trigger to perform properly? What if I have 20 other arrays that could potentially have outlier data as well? Is there any reason I should not move the outlier logic to a trigger?

Cheers!