0

For the first time I want to see other database than SQL Server for my development.

MongoDB is a good alternative for my project.

What is difficult to understand is "the design" of MONGODB for me.

User (table)

Id
Name
Address

Friends (table)

IdUser
IdFriends

With this SQL table, I want to allow an user to became a friend of other user like Facebook.

Which is the best design for MongoDB?

1 Answer 1

3

Consider nesting friends ids inside User document:

{
    _id: 7,
    name: "Johnny",
    address: "Acme St.",
    friends: [8, 9, 15]
}

On one hand you don't need joins to fetch friends (at least ids), because everything is nested within a single document and the schema looks more pleasant. On the other - to fetch the actual friends, you hit N+1 problem. However fetching by id is rather cheap, also consider caching.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, suppose that i have 1.000 or 10.000 friends?
MongoDB document size limit is 16 MiB (SERVER-431), also there are operations to work with nested arrays without fetching whole array itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.