Reputation: 14834
Let's say I have a collection called UserInfo
contains:
userName
: "john doe"
userID
: "100"
I also a have collection called Friends
contains:
userID
: "100"
friendID
: "200"
Each user can have multiple friends. So I attempt to add another:
userID
: "100"
friendID
: "300"
and so on.
Is this the right way to do this? I need the ability to add and remove friends from a user.
Another approach I can think of for UserInfo
:
userName
: "john doe"
userID
: "100"
friends
: "200, 300, 400" //etc.
Or other solutions?
Upvotes: 1
Views: 50
Reputation: 35348
Well there is a whole section in the mongoDB docs talking about the pros and cons of embedding documents vs referencing them in a separate collection.
It really depends on the scenario and requirements (in short: embedded documents allow for atomic operations and in general some performance gains but may cause duplication).
In your specific case I would most probably go with an embedded array of friend IDs (your last shown option) and not use a separate Friends
collection as storing the friend's IDs causing no data duplication anyway.
Upvotes: 1