16

i am making an web application and using a mongodb for my database. but im new in mongodb and i just want to know if i still needed to normalize my database like what others do in using RDBMS. that before making a table or database it is normalize.

1 Answer 1

28

Normalizing your data like you would with a relational database is usually not a good idea in MongoDB.

Normalization in relational databases is only feasible under the premise that JOINs between tables are relatively cheap. The $lookup aggregation operator provides some limited JOIN functionality, but it doesn't work with sharded collections. So joins often need to be emulated by the application through multiple subsequent database queries, which is very slow (see question MongoDB and JOINs for more information).

For that reason, you should design your database in a way that the most common queries can be satisfied by querying a single collection, even when this means that you will have some redundancy in your database. A good tool to model 1:n relations are often arrays of sub-objects. However, try to avoid objects which grow indefinitely over time, because in that case they will be moved around the file a lot which will impact write performance.

7
  • 1
    so you are saying normalizing the a database when using a mongoDB is not a good practice? Jul 19, 2014 at 10:17
  • @heathcliff usually yes, but there are some edge-cases where normalization can be necessary.
    – Philipp
    Jul 19, 2014 at 10:20
  • 3
    If somwone is reading this, join operations can be done in MongoDb
    – Ori Refael
    Feb 15, 2018 at 21:03
  • 2
    @MohitAngiras The $lookup operator didn't exist yet in 2014 when I wrote this answer. Thanks for calling my attention to this answer. I updated it.
    – Philipp
    Apr 29, 2018 at 13:14
  • 1
    @nsantiago2719 The spirit of the message is "Depends", even though it kinda conflicts with the first sentence. If you consider sharding, then it's not a good practice. Otherwise, it depends on the usage. If you want to avoid big objects passing around, then normalize it. If you have many JOINs, don't do it. It entirely depends on the way you use the data and which expensive operation is more frequent.
    – GeorgiG
    Jul 9, 2019 at 10:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.