I have built a simple MERN app where users can rate phone numbers. Users just fill in the phone number, choose rating (1 - 5 star rating), their city & short text. The app has search function with filter & sorting options. It all works good enough ATM but I think it might break when multiple concurrent users use the website because I update the phone number model (mobileSchema) after a rating (messageSchema) has been submitted - using Mongoose middlewares (post hooks).

For example, I need to calculate number of ratings (messagesCount) for phone number. I use Message.countDocuments({ mobile: mobile._id }) for that. However, I also need to update other properties of phone number (mobileSchema - lastMessageDate, globalRating, averageRating) so that operation takes some time. I believe the number of ratings might not be right when 2 users submit rating at the same time - it will increment the number of ratings (messagesCount) by 1 instead of 2.

Is there a better approach? Can a post hook be fired after the previous post hook already finished?

Not directly mongoose related but here is a discussion about write operations. Based on that information I would say the post hook fires after the write operation is finished but I didn't check mongoose documentation.
– JanneDec 1 '18 at 14:04

3 Answers
3

I think you are always going to run into async issues with your approach. I don't believe you can "synchronize" the hooks; seems to go against everything that is true about MongoDB. However, at a high level, you might have more success grabbing the totals/summaries at run-time, rather than trying to keep them always in sync. For instance, if you need the total number of messages for a given mobile device, why not:

Messages.find({mobile: mobile._id})

and then count the results? That will save you storing the summaries and keeping them updated. However, I also think your current approach could work, but you probably need to scrap the "countDocuments". Something a bit more async friendly, like:

Ultimately I think your design would be strengthened if you stored Messages as an array inside of Mobile, so you can just push the message on it. But to answer the question directly, the aggregation should keep everything tidy.

"grabbing the totals/summaries at run-time": I do not think that would be performance friendly. I would have to calculate all properties (messagesCount, lastMessageDate, globalRating, averageRating...) on each request from client. So simple filter & sort would take too long. "stored Messages as an array inside of Mobile": I am afraid that it could grow above max MongoDB document size - 16 MB.
– JihlavankaDec 1 '18 at 14:40

1

Fair enough. The 2nd point is well taken, but regarding the 1st point, have you considered using caching like Redis? Caching in the database seems like a lot of strain.
– piisexactly3Dec 1 '18 at 14:54

I will calculate all the values I need (messagesCount, globalRating etc.) in post hook and then I will check if the mobile document has the same __v (version) value during final findOneAndUpdate operation (because this operation locks the document and can increment __v). If it has different __v then I will call the post hook again to ensure it will calculate the right values.