In MERN Social, the users will be able to follow each other. Each user will have a list of followers and a list of people they follow. Users will also be able to see a list of users they can follow; in other words, the users in MERN Social they are not already following.
In order to keep track of which user is following which other users, we will have to maintain two lists for each user. When one user follows or unfollows another user, we will update one's following
list and the other's followers
list.
To store the list of following
and followers
in the database, we will update the user model with two arrays of user references.
mern-social/server/models/user.model.js
:
following: [{type: mongoose.Schema.ObjectId, ref: 'User'}], followers: [{type: mongoose.Schema.ObjectId, ref: 'User'}]
These references will point to the users in the collection being followed by or following the given user.