Goalie
Goalie

Reputation: 3105

How do you decide when to create a collection versus a embedded document in MongoDB?

I'm currently in the process of learning noSQL via MongoDB but my schooling and work experience is with relational databases.

I currently have a Users collection and I need to implement functionality that stores the amount of webpage hits for the user profile page, and around 5 more stats.

Should I create a new collection to store this information or should I create an embedded document to store these 6 pieces of information in the Users collection?

Thanks!

Upvotes: 0

Views: 451

Answers (1)

jdi
jdi

Reputation: 92617

Create embedded documents until they just don't make sense anymore to include under one collection. A stats related to a User document makes 100% sense to store embedded. Simply put...a User profile has stats. The stats don't make any sense for any other collection.

If you end up having a collection that really needs references because many different collections might reference a single document, then it makes sense to split that into a collection and use id references. When you start thinking about having to update many other documents when you change something in one embedded document, then it might be good to move to a unique collection. That way each document only refers to an id, which can change its document data at any time.

Upvotes: 3

Related Questions