What might the pros and cons for Embedded Arrays VS flat documents in mongodb?









up vote
0
down vote

favorite












I am a newbie to mongodb , I have a scenario where there will be (example)



  1. Users collection

  2. Each Users can have maximum of 10 habits .

There is two options infront of me



  1. Create a collection like UserHabits and add the habits as embedded
    array
    .

  2. Add each habits as each documents .

What might be the cons and pros for these two approaches . Thanks .










share|improve this question





















  • mongodb.com/blog/post/…
    – Alex Blex
    Nov 9 at 14:22














up vote
0
down vote

favorite












I am a newbie to mongodb , I have a scenario where there will be (example)



  1. Users collection

  2. Each Users can have maximum of 10 habits .

There is two options infront of me



  1. Create a collection like UserHabits and add the habits as embedded
    array
    .

  2. Add each habits as each documents .

What might be the cons and pros for these two approaches . Thanks .










share|improve this question





















  • mongodb.com/blog/post/…
    – Alex Blex
    Nov 9 at 14:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am a newbie to mongodb , I have a scenario where there will be (example)



  1. Users collection

  2. Each Users can have maximum of 10 habits .

There is two options infront of me



  1. Create a collection like UserHabits and add the habits as embedded
    array
    .

  2. Add each habits as each documents .

What might be the cons and pros for these two approaches . Thanks .










share|improve this question













I am a newbie to mongodb , I have a scenario where there will be (example)



  1. Users collection

  2. Each Users can have maximum of 10 habits .

There is two options infront of me



  1. Create a collection like UserHabits and add the habits as embedded
    array
    .

  2. Add each habits as each documents .

What might be the cons and pros for these two approaches . Thanks .







mongodb






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 14:04









Sachin

1,2471825




1,2471825











  • mongodb.com/blog/post/…
    – Alex Blex
    Nov 9 at 14:22
















  • mongodb.com/blog/post/…
    – Alex Blex
    Nov 9 at 14:22















mongodb.com/blog/post/…
– Alex Blex
Nov 9 at 14:22




mongodb.com/blog/post/…
– Alex Blex
Nov 9 at 14:22












1 Answer
1






active

oldest

votes

















up vote
0
down vote













With a one:few relationship in MongoDB, it is almost always more beneficial to embed the doc for performance reasons. The only cons to working with embedded docs are:



  1. If for some reason you open the habits option up to say where a person could select 1,000,000 habits, the document might exceed the maximum size of 16MB.

  2. You open yourself up to inconsistency if a user can enter different values into habits (ex. One user's habit it 'flying a kite' and one user's habit is 'kite flying'). However, if you have normalized data, this won't be a problem.





share|improve this answer










New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Thanks for the catch! 16MB :)
    – Nick B.
    Nov 9 at 20:41











  • RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
    – Sergio Tulentsev
    Nov 9 at 20:42











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227204%2fwhat-might-the-pros-and-cons-for-embedded-arrays-vs-flat-documents-in-mongodb%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













With a one:few relationship in MongoDB, it is almost always more beneficial to embed the doc for performance reasons. The only cons to working with embedded docs are:



  1. If for some reason you open the habits option up to say where a person could select 1,000,000 habits, the document might exceed the maximum size of 16MB.

  2. You open yourself up to inconsistency if a user can enter different values into habits (ex. One user's habit it 'flying a kite' and one user's habit is 'kite flying'). However, if you have normalized data, this won't be a problem.





share|improve this answer










New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Thanks for the catch! 16MB :)
    – Nick B.
    Nov 9 at 20:41











  • RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
    – Sergio Tulentsev
    Nov 9 at 20:42















up vote
0
down vote













With a one:few relationship in MongoDB, it is almost always more beneficial to embed the doc for performance reasons. The only cons to working with embedded docs are:



  1. If for some reason you open the habits option up to say where a person could select 1,000,000 habits, the document might exceed the maximum size of 16MB.

  2. You open yourself up to inconsistency if a user can enter different values into habits (ex. One user's habit it 'flying a kite' and one user's habit is 'kite flying'). However, if you have normalized data, this won't be a problem.





share|improve this answer










New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Thanks for the catch! 16MB :)
    – Nick B.
    Nov 9 at 20:41











  • RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
    – Sergio Tulentsev
    Nov 9 at 20:42













up vote
0
down vote










up vote
0
down vote









With a one:few relationship in MongoDB, it is almost always more beneficial to embed the doc for performance reasons. The only cons to working with embedded docs are:



  1. If for some reason you open the habits option up to say where a person could select 1,000,000 habits, the document might exceed the maximum size of 16MB.

  2. You open yourself up to inconsistency if a user can enter different values into habits (ex. One user's habit it 'flying a kite' and one user's habit is 'kite flying'). However, if you have normalized data, this won't be a problem.





share|improve this answer










New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









With a one:few relationship in MongoDB, it is almost always more beneficial to embed the doc for performance reasons. The only cons to working with embedded docs are:



  1. If for some reason you open the habits option up to say where a person could select 1,000,000 habits, the document might exceed the maximum size of 16MB.

  2. You open yourself up to inconsistency if a user can enter different values into habits (ex. One user's habit it 'flying a kite' and one user's habit is 'kite flying'). However, if you have normalized data, this won't be a problem.






share|improve this answer










New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited Nov 9 at 20:41





















New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Nov 9 at 20:37









Nick B.

114




114




New contributor




Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Nick B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Thanks for the catch! 16MB :)
    – Nick B.
    Nov 9 at 20:41











  • RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
    – Sergio Tulentsev
    Nov 9 at 20:42

















  • Thanks for the catch! 16MB :)
    – Nick B.
    Nov 9 at 20:41











  • RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
    – Sergio Tulentsev
    Nov 9 at 20:42
















Thanks for the catch! 16MB :)
– Nick B.
Nov 9 at 20:41





Thanks for the catch! 16MB :)
– Nick B.
Nov 9 at 20:41













RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
– Sergio Tulentsev
Nov 9 at 20:42





RE point 2: this inconsistency depends on how user inputs the habits. If it's a free-form text input (which allows users to create new habits), such inconsistency is bound to happen, regardless of how it's stored. On the other hand, if it's a fixed dropdown (with a server-side inclusion validation), then it won't happen (again, regardless of storage shape)
– Sergio Tulentsev
Nov 9 at 20:42


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227204%2fwhat-might-the-pros-and-cons-for-embedded-arrays-vs-flat-documents-in-mongodb%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo