0

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats great, thanks. Yeah and in most cases you wouldn't require company and *all employees to be loaded in the same request.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.