Burak
Burak

Reputation: 5764

How to model friendship in MongoDB?

I want to use MongoDB and Node.js in my project. What I need is, modelling the db for friendship relationships(User1 follow User2). Ina relational DB it is easy, but I dont know how to do this with MongoDB. I will also use REST API so I should be able to make relationships via REST api

Edit:

Is it a good way?

following: [[{ type: Schema.Types.ObjectId, ref: 'User' }]]

If I do that, should I save both follower and followed? I am getting too many recursion error.

Upvotes: 1

Views: 2419

Answers (2)

Raxit Sheth
Raxit Sheth

Reputation: 2601

What all operations you want to do with that?

you may want to check Twitter-like app using MongoDB

Upvotes: 1

Sam Holder
Sam Holder

Reputation: 32946

can you not just maintain a list of FollowedBy and Following user ids on each user and just update those lists whenever new relationships are added?

Effectively you are denormalising that data due to lack of relational support.

Upvotes: 1

Related Questions