0

Kindly I want to design a new db using Mongodb with features like :

    {[
    name:company1, 
    password:123,
    type:company,
    .
    .
    .
    categories{
    Mobile device, 
    Computer devices
    ],
    [
    name:ihab salem,
    password:123,
    type:user,
    .
    .
    .
    intersted_category{
    Home Appliance,
    Mobile Devices
    }]
    .
    .
    .
    }
    
so kindly in this case should i use Embed Colection as i described above, or should i seperate them into many collections " collection for Categories, another collection for users and another one for companies...etc".?
Ihab Salem
  • 83
  • 5

1 Answers1

1

It strongly depends on your data and how you're going to use it. This thread has a brilliant explanation on general rules. And here you can find official recommendations. Shortly:

  • in general, embedding gives you better performance in read operations
  • write operations will be atomic with embedded documents
  • embedding related data in one document can lead to situation, where documents grow after creation - that can impact write performance
  • if your data is planned to be used in different collections - with references model you will need update them in only one place
  • complex hierarchies could be more flexible with references
  • using references, client application could require several round trips to server
  • updates are not atomic for several documents, so you should think about data consistency when working with references model

So, in your case I'd put in one document as much as I could, especially if your embedded documents are small ones - till the point there it could broke "natural" model of data for your system, or embedded documents tends to grow quickly.

Community
  • 1
  • 1
Natalja Olefire
  • 442
  • 5
  • 9