Hey @Joseph_Gan!
Great questions! I’ll be thinking about how we can answer these better in the future. In the meantime, let’s get to your specific questions.
As with almost everything in MongoDB, the answer is “it depends on your use case.” I’ll walk through one possible solution, but know that there may be a better way based on your use case.
Regarding the extended-reference pattern question:
One option would be to do handle the updates at the application level. You could do something like db.collA.updateMany({'epd.profile_key': '12345'},{$set: {'epd.profile_name': 'new'}})
whenever you make an update to the profile name. You’d have to ensure that your development team knew about this data duplication and handled it correctly every time.
Another option would be to use change streams or triggers. These allow you to watch for updates in a collection and take a specified action whenever an update occurs. This would prevent you from worrying about forgetting to update the documents in your application code every time.
Another thing to consider is how detrimental data inconsistency would be. You may want to wrap your updates in a transaction to ensure that the update occurs in all of the collections or none of the collections.
Regarding the Subset Pattern question:
Again, the best way to do this will depend on how you’ve implemented it and your particular use case.
I’d likely keep all of the information about every review in the reviews collection – regardless of whether it’s in the movies collection or not. I’d keep the movie name and rating in the reviews array in the movies collection. Whenever you’re updating the movie reviews, check to see if the review is high enough to add the item to the movies collection. If so, remove the bottommost review and add the new review.
Again, we could consider whether using a transaction, change stream, and/or trigger would help.