Reputation: 261
Consider I have the following models: Company & Employee
I would create the base CompanySchema as follows:
{
"CompanyId":Guid,
"Name":String,
"IconUrl":String,
"Employees":[{
"EmployeeId":Guid,
"Name":String,
"EmployeeIds":[]
}]
}
So Employees would be in a nested collection and they need to have direct relationships (connected) with other employees.
If for instance, employees could get upwards of 50,000 records per company. Would I be better of having 2 separate collections or 1 nested collection?
If 2 collections I could cache the company to use IconUrl on employees when they are looked up. I'd also like to do sorting on employees cross company.
It would be great if anyone who has done testing or has previous experience could share their wisdom!
Upvotes: 0
Views: 1824
Reputation: 12240
In your example it probably makes more sense to create two separate collections.
Embedding documents is very useful in some cases, but it's not a silver bullet. MongoDB has a limit on a document size that is 16 MB per document. So if you know that your array will contain a lot of sub-documents, it's probably wiser to split it in two collections.
Also, working with embedded documents is not as straightforward as with regular documents (e.g. paging and updating multiple sub-documents)
When you're designing your documents it's useful to think about the intended usage i.e. do you always need to load all employees for a company or do you need to load individual employees more often? How about updating them?
You can read more about Data Modeling on MongoDB docs.
Upvotes: 2