If you have a Users collection and there is a many to many relationship, such as subscriber and subscribed, then you have 2 fields to hold these 2 distinct participants of this relationship.
However, there is no particular relationship between the 2 friends, how do you name the fields? It’s not predictable in what field to look for a particular member?
Hi @Big_Cat_Public_Safety_Act ,
Not sure what exactly your question is?
Usually many to many will be stored as reference arrays inside the related documents:
// Profiles
{
_id : "A",
name : "aaa",
"subscribedFriends" : [ { "_id" : "B", name : "bbb" } , { "_id" : "C", name : "ccc"}],
"followingFriends" : [{"_id" : "D", name : "ddd" }]
}
{
_id : "B",
name : "bbb",
"subscribedFriends" : [ { "_id" : "D", name : "ddd" } , { "_id" : "C", name : "ccc"}]
"followingFriends" : [ { "_id" : "A", name : "aaa" } ]
}
...
To avoid outliers where users have thousands of followers or subscribers we use the outlier pattern:
Does that answer your question?
Thanks,
Pavel
The question is in the event of the outlier where the data needs to be stored in a separate collection. And also, there is no distinction between the members of the relationship. In your example, you have subscribedFriends
and followingFriends
. In my case, it’s just friends
. Both members are just friends.
Ok so they will apear in each others lists:
{
_id : "A",
name : "aaa",
"friends" : [ { "_id" : "B", name : "bbb" } , { "_id" : "C", name : "ccc"} ... 500 friends],
bucketFriends : 500
}
{
_id : "A-outlier-1",
name : "aaa",
"friends" : [ { "_id" : "X" ... 400 friends],
bucketFriends : 400
}
{
_id : "B",
name : "bbb",
"friends" : [ { "_id" : "A", name : "aaa" } ]
}
Here the outlier sit in the same collection so _id : “A” , and its outlier : “A-outlier-a” are still in the same collection to get all friends of “A” run:
db.collection.find({ _id : /^A/ })
Thanks
Pavel