Vineet
Vineet

Reputation: 375

Normalization in MongoDB

I have a document structured like this

ITEM:

objectID
itemDesc
categoryID
categoryName
subCategoryID
subCategoryName,
price
flavor
itemDesc
...

Though generally MongoDB does not encourage normalization, I am concerned if there would be data integrity issues if I don't normalize this. I expects hundreds of thousands of records (possibly millions) in this application.

Is this schema a better option?

CATEGORY:

categoryID
categoryName

SUBCATEGORY:

subCategoryID
subCategoryName,

ITEM:

objectID
itemDesc
categoryID
subCategoryID
price
flavor
itemDesc

Upvotes: 1

Views: 1775

Answers (1)

vladzam
vladzam

Reputation: 5908

It all depends on how the end-user will interact with your application.

From what you have commented up until now, I understand that your queries will most likely fall into one of the below categories:

  • Bring all items from category X
  • Bring all items from subcategory X
  • Bring all items that are part of the X category and Y subcategory

If that is the case, I think that it would be best if you use just use one collection to store your data, as your structure is not that complex and there are no complex many-to-many relationships.

As stated in the official MongoDB Data Model Design documentation:

In general, use normalized data models:

when embedding would result in duplication of data but would not provide sufficient read performance advantages to outweigh the implications of the duplication.

to represent more complex many-to-many relationships.

to model large hierarchical data sets.

Regarding performance, I think that it is better to create indexes that fit your query pattern and fetch data from one collection instead of having to query multiple collections.

Upvotes: 1

Related Questions