Hi, I’m trying to set up a Notifications system. I have a Notifications collection which contains the following data:
{
"_id": "newNotificationID",
"userID": "someUserID",
"type": "typeOfNotification"
...
}
For each User, they may have a number of followers, and I have stored the follower’s userIDs in an array as a field of the User’s document:
{
"_id": "someUserID",
"followers": ["followerA_ID", "followerB_ID", ... ],
...
"notifications": ["notification1_ID", ... ]
}
How should I be bulk updating all the documents matching the respective follower ID’s, and pushing the newNotificationID
to this Notifications array? When a new Notification is generated by a user, I need to update that user’s entire followers list.
Can this only be done in a loop, by iterating over each follower, or is there a faster way to update multiple documents at the same time? So when someUserID
does an action and I generate a newNotificationID
, I need to push newNotificationID
to ALL of somerUserID’s follower’s User documents. So I need to push it to followerA_ID
's notifications array, followerB_ID
's notifications array, so on and so forth.
I’m not sure if this is a good design in the first place, but if I need to do it this way how should I be going about it?