jaffer_syed
jaffer_syed

Reputation: 171

Is it a good idea to have unique keys to better aggregate data in MongoDB

Hello I am creating an app where people essentially join groups to do tasks, and each group has a unique name. I want to be able to update each of the users document that has to do with a specific group without having to for loop each user and update with each iteration.

I want to know if its a good idea to have a unique key like this in mongoDB.

{
...
"specific_group_name": (whatever data point here)
...
}

in each of the users document, so I can just call a simple

updateToMany(eq("specific_group_name",  (whatever data point here)), Bson object)

To decrease the run time that is involved, just in case there is alot of users within the group.

Thank you

Upvotes: 0

Views: 26

Answers (1)

Tan Kim Loong
Tan Kim Loong

Reputation: 1035

Just a point to note, instead of a specific group name, better make sure that it's specific groupId. Also pay special attention to cases when you have to remove group from the people, and also if there's cases when a person in a particular group shouldn't receive this update.

What you want to do is entirely valid though. If you put specific_group_name/id in the collection, then you're moving the selection logic to db. If you're doing a one-by-one update, then you have more flexibility on how to select users to update on Java/application side.

If selection is simple (a.k.a always update people in this group) then go ahead

Upvotes: 1

Related Questions