Reputation: 11641
Which way is the right way to store nested data in mongodb?
I have articles application, and each article belong to subdomain and each subdomain belong to domain.
my website look like that:
/home -> list all domains
/home/domain -> list all subdomains
/home/domain/subdomain -> list all my articles
/home/domain/subdomain/article -> show the article details
Which of there structure follow the best practies in mongodb?
Option 1
Create each collection and link bewteen them by using ref object id
domains (collection) [{ name }]
subdomains (collection) [{ name, domain }]
articles (collection) [{ name, subdomain }]
Option 2
create a article collection (my main model I think) and inside I have domain with subdomain
articles (collection) [{ name, domain: { name, subdomain: { name } } }]
Option 3
By creating domains collections inside I will have subdomain and inside (subdomain) I'll have articles that belong to subdomain.
domains (collection) [{ name, subdomain: [{ name, articles: [{ name }] }] ]
Upvotes: 0
Views: 288
Reputation: 1318
Option 1 is the best for your case.
Mongodb has document size limit of 16mb and in your case, there are probably going to be multiple articles associated with a single sub domain. So embedding all those articles in a single document will eventually fill that document limit.
Also, mongodb is designed to perform optimized queries over multiple documents, and that should be the design scheme you should go for if you have many documents to embed.
Also, take a look at this article for best practices for schema design https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1
Upvotes: 1