简体   繁体   中英

Efficient way to keep track of updating multiple mongodb collections

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!

Sounds like a trigger but MongoDB doesn't support those. There is another way though. Check out this answer in another thread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM