Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing favorites list for each user in mongo?

I am working with movie app using node, express and mongo. How do I store the favorite movie list for each user in mongo database?

like image 930
Navin prasad Avatar asked Mar 09 '23 19:03

Navin prasad


1 Answers

Here you have two alternatives:

  • Embedding: that is, to include into a user document the list of its favorite movies
  • Split collections: use two separate collections for users and movies, referencing them through id

Which one to choose?

You should design your MongoDB collections based on the access strategy of your application.

In general, you should go for embedding movies into user doc if:

  • embedded data (movies) are always retrieved when user info are retrieved. If you are always retrieving user info but rarely you need also movies data then embedding cannot be so good.
  • embedded data are not frequently subject to modifications
  • the cardinality is not too big (remember MongoDB document max size is 16MB). In this case I think cardinality of favorite movies per user could be ok to go for embedding them, but if just as example you have 100K movies per user you are forced to split the collections
  • Data Integrity can be an issue: having movies embedded into user docs, you can take advantage of document level locking (if you are using Wired Tiger storage engine). Otherwise, if you're in a concurrent system, with the two collections design you have to handle locking mechanism by yourself at application level.
like image 50
Davis Molinari Avatar answered Mar 15 '23 04:03

Davis Molinari