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)
- Users collection
- Each Users can have maximum of 10 habits .
There is two options infront of me
- Create a collection like UserHabits and add the habits as embedded
array . - Add each habits as each documents .
What might be the cons and pros for these two approaches . Thanks .
mongodb
add a comment |
up vote
0
down vote
favorite
I am a newbie to mongodb , I have a scenario where there will be (example)
- Users collection
- Each Users can have maximum of 10 habits .
There is two options infront of me
- Create a collection like UserHabits and add the habits as embedded
array . - Add each habits as each documents .
What might be the cons and pros for these two approaches . Thanks .
mongodb
mongodb.com/blog/post/…
– Alex Blex
Nov 9 at 14:22
add a comment |
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)
- Users collection
- Each Users can have maximum of 10 habits .
There is two options infront of me
- Create a collection like UserHabits and add the habits as embedded
array . - Add each habits as each documents .
What might be the cons and pros for these two approaches . Thanks .
mongodb
I am a newbie to mongodb , I have a scenario where there will be (example)
- Users collection
- Each Users can have maximum of 10 habits .
There is two options infront of me
- Create a collection like UserHabits and add the habits as embedded
array . - Add each habits as each documents .
What might be the cons and pros for these two approaches . Thanks .
mongodb
mongodb
asked Nov 9 at 14:04
Sachin
1,2471825
1,2471825
mongodb.com/blog/post/…
– Alex Blex
Nov 9 at 14:22
add a comment |
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
add a comment |
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:
- 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.
- 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.
New contributor
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
add a comment |
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:
- 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.
- 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.
New contributor
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
add a comment |
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:
- 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.
- 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.
New contributor
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
add a comment |
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:
- 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.
- 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.
New contributor
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:
- 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.
- 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.
New contributor
edited Nov 9 at 20:41
New contributor
answered Nov 9 at 20:37
Nick B.
114
114
New contributor
New contributor
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
mongodb.com/blog/post/…
– Alex Blex
Nov 9 at 14:22