Hey Sandeep Panda ,
What would be the downside of storing it in the user details or if required in a separate collection in form or array?
{
.
.
.
followers: [ObjectId],
follows: [ObjectId],
}
This would eliminate aggregation query in a junction collection. Every time user_1 follows user_2 :
- Add id of
user_2 in follows of user_1
- Add id of
user_1 in followers if user_2.
If you also want to maintain the date-time of following, you can add
{
id: ObjectId,
dateAdded: 'Date'
}
as the object in the array.
If required to kept out of user collection, it can be made a standalone collection of its own with reference to user id:
{
userId: ObjectId,
followers: [ObjectId], // or [{id: ObjectId, dateAdded: 'Date'}]
follows: [ObjectId], // or [{id: ObjectId, dateAdded: 'Date'}]
}
Hi Shiva!
For architecting "Follow/Following" system in MongoDB, your best bet is probably creating a junction collection (Many-Many table in relational DBs). For example, you could have a collection with documents that have the following schema:
{ from: { type: ObjectId }, // Indicates who's is following to: { type: ObjectId } , // Who's being followed dateAdded: 'Date', isActive: 'Boolean' }Or if you need a more sophisticated approach, use a graph database that gives you more freedom and flexibility. The only downside is that you will introduce another moving part into your app and you should have enough knowledge of the graph DB.
IMO, if you are just starting out go with MongoDB (assuming this is the DB of your choice), and once you hit any bottlenecks or limitations, start searching for other solutions.