AKASH
AKASH

Reputation: 416

Best way to store data in mongodb

I am developing a project where there are lots of relations are there. I want to know which one is best method to store data.

  1. create child_model and then push _id to parent_model.
  2. create child_model inside parent model, we are pushing whole data not just _id.

Upvotes: 0

Views: 79

Answers (1)

Reto
Reto

Reputation: 3141

There is no "best way", both are sometimes the best. It really depends on your use case and questions like these:

  • do you need to load or store your children sometimes separately without loading your parent? -> separate models.
  • or will you need to load the parent anyway all the time when you need a child? -> same model
  • can your child always only belong to exactly one parent? -> same model
  • will multiple parents or other objects ever reference a child? -> separate models
  • is the lifetime of your child always identical to the lifetime of your parent -> same model
  • can the lifetimes be different? can your child ever become "detached" from your parent -> separate models.

think about those questions and decide, whats best for your use case.

Upvotes: 1

Related Questions