Reputation: 13721
I am building an event registration app and storing the data in MongoDB. I have 3 collections that relate to each other via a 'foreign key' and whenever one of the collections get updated, the rest need to get updated as well. Therefore, I was wondering if there's a pattern or method to easily keep track of all the collections that have to be updated or if theres a way for a collection to 'listen' to when a dependent collection updates, and automatically update itself.
Here's a simplied example:
I have collections for Table, User, and Group.
Table:
{
table_id
table_count
}
User:
{
userId,
table_id,
group_id
table_count
}
Group:
{
group_count
table_id
}
Whenever a User registers, I need to update the table_count
and group_count
fields. Likewise, if a User changes tables or groups, the above fields will need to be updated as well. Even with only 3 collections, I need to write 6 update statements to consider all the possible user actions (join table, leave table, etc.). So suppose an application had 10, 50, or 100 collections that all need to be updated whenever one of them updates, is there an efficient way to keep track/make all the updates?
Thanks!
Upvotes: 1
Views: 131
Reputation: 139
Sounds like a trigger but MongoDB doesn't support those. There is another way though. Check out this answer in another thread.
Upvotes: 2