Home > OS >  In MongoDB, what is the purpose of embedded documents if the same thing can be achieved without usin
In MongoDB, what is the purpose of embedded documents if the same thing can be achieved without usin

Time:02-08

For example

Instead of a field named, COLUMN_1, that holds an embedded document with fields A, B, and C.

Why not 3 separate fields with names COLUMN_1_A, COLUMN_1_B, COLUMN_1_C.

CodePudding user response:

In mongoDB you prefer to work with documents the way they exist naturaly , afcourse you can create routines that can translate every time and assemble/disasemble the document:

  COLUMN_1:{A:X,B:Y,C:Z}

to:

 COLUMN_1_A:X,COLUMN_1_B:Y,COLUMN_1_C:Z

But this is additonal work that you dont want to do every time , you are a lazy effective developer that prefer just store your json document the way it will be used unless there is a specific use case that make sence to do it this way ... :)

Also please, note embedding is possible up to 100 levels , but max document size is still limited to 16MB , so in your document model it is not expected that you embed all your database in single document...

When you need performance , you add indices and optimize queries , that way your most searched data stay in memory , there is no big difference if your fields are embeded or in the root if they are not indexed ...

  •  Tags:  
  • Related