MongoDB Collection Structure











up vote
0
down vote

favorite












I have a collection named User.



User has many information that can be grouped together example:



{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}


So I want to know what the best practice is



1. First way, Using nested fields:

{user:
{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}
}

2. Second way: Just put them all together.

{user:
street:"xx",
city:"xx",
...
school:"xx",
job: "xx",
...
}


First way is obviously more readable for humans and it makes it easier for human to find relevant information.



What are the downside to grouping/nesting data like the first way?
Does it make querying of nested fields slower? Indexing issues? Any idea?










share|improve this question
























  • mongodb is not OODB, so it is not designed to store objects, even though it is possible. what benefit do you get from storing "address" as an object that contains street and city? it would be much easier to store it the 2nd way, flatten, so queries can be simpler.
    – OhadR
    Nov 11 at 8:44










  • i guess it's more readable the first way. Information can be grouped accordingly, reading relevant data is easier. But is there any performance downside?
    – user1955934
    Nov 11 at 9:59










  • Possible duplicate of Is there any performance difference for nested document in mongodb query
    – OhadR
    Nov 11 at 10:25










  • Right, yes i want to confirm the answer was no difference?
    – user1955934
    Nov 11 at 10:40















up vote
0
down vote

favorite












I have a collection named User.



User has many information that can be grouped together example:



{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}


So I want to know what the best practice is



1. First way, Using nested fields:

{user:
{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}
}

2. Second way: Just put them all together.

{user:
street:"xx",
city:"xx",
...
school:"xx",
job: "xx",
...
}


First way is obviously more readable for humans and it makes it easier for human to find relevant information.



What are the downside to grouping/nesting data like the first way?
Does it make querying of nested fields slower? Indexing issues? Any idea?










share|improve this question
























  • mongodb is not OODB, so it is not designed to store objects, even though it is possible. what benefit do you get from storing "address" as an object that contains street and city? it would be much easier to store it the 2nd way, flatten, so queries can be simpler.
    – OhadR
    Nov 11 at 8:44










  • i guess it's more readable the first way. Information can be grouped accordingly, reading relevant data is easier. But is there any performance downside?
    – user1955934
    Nov 11 at 9:59










  • Possible duplicate of Is there any performance difference for nested document in mongodb query
    – OhadR
    Nov 11 at 10:25










  • Right, yes i want to confirm the answer was no difference?
    – user1955934
    Nov 11 at 10:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a collection named User.



User has many information that can be grouped together example:



{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}


So I want to know what the best practice is



1. First way, Using nested fields:

{user:
{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}
}

2. Second way: Just put them all together.

{user:
street:"xx",
city:"xx",
...
school:"xx",
job: "xx",
...
}


First way is obviously more readable for humans and it makes it easier for human to find relevant information.



What are the downside to grouping/nesting data like the first way?
Does it make querying of nested fields slower? Indexing issues? Any idea?










share|improve this question















I have a collection named User.



User has many information that can be grouped together example:



{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}


So I want to know what the best practice is



1. First way, Using nested fields:

{user:
{address : {street:"xx", city:"xx"}}
{history: {school:"xx", job: "xx"}}
{..}
}

2. Second way: Just put them all together.

{user:
street:"xx",
city:"xx",
...
school:"xx",
job: "xx",
...
}


First way is obviously more readable for humans and it makes it easier for human to find relevant information.



What are the downside to grouping/nesting data like the first way?
Does it make querying of nested fields slower? Indexing issues? Any idea?







mongodb mongodb-query spring-data-mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:22

























asked Nov 11 at 4:23









user1955934

242321




242321












  • mongodb is not OODB, so it is not designed to store objects, even though it is possible. what benefit do you get from storing "address" as an object that contains street and city? it would be much easier to store it the 2nd way, flatten, so queries can be simpler.
    – OhadR
    Nov 11 at 8:44










  • i guess it's more readable the first way. Information can be grouped accordingly, reading relevant data is easier. But is there any performance downside?
    – user1955934
    Nov 11 at 9:59










  • Possible duplicate of Is there any performance difference for nested document in mongodb query
    – OhadR
    Nov 11 at 10:25










  • Right, yes i want to confirm the answer was no difference?
    – user1955934
    Nov 11 at 10:40


















  • mongodb is not OODB, so it is not designed to store objects, even though it is possible. what benefit do you get from storing "address" as an object that contains street and city? it would be much easier to store it the 2nd way, flatten, so queries can be simpler.
    – OhadR
    Nov 11 at 8:44










  • i guess it's more readable the first way. Information can be grouped accordingly, reading relevant data is easier. But is there any performance downside?
    – user1955934
    Nov 11 at 9:59










  • Possible duplicate of Is there any performance difference for nested document in mongodb query
    – OhadR
    Nov 11 at 10:25










  • Right, yes i want to confirm the answer was no difference?
    – user1955934
    Nov 11 at 10:40
















mongodb is not OODB, so it is not designed to store objects, even though it is possible. what benefit do you get from storing "address" as an object that contains street and city? it would be much easier to store it the 2nd way, flatten, so queries can be simpler.
– OhadR
Nov 11 at 8:44




mongodb is not OODB, so it is not designed to store objects, even though it is possible. what benefit do you get from storing "address" as an object that contains street and city? it would be much easier to store it the 2nd way, flatten, so queries can be simpler.
– OhadR
Nov 11 at 8:44












i guess it's more readable the first way. Information can be grouped accordingly, reading relevant data is easier. But is there any performance downside?
– user1955934
Nov 11 at 9:59




i guess it's more readable the first way. Information can be grouped accordingly, reading relevant data is easier. But is there any performance downside?
– user1955934
Nov 11 at 9:59












Possible duplicate of Is there any performance difference for nested document in mongodb query
– OhadR
Nov 11 at 10:25




Possible duplicate of Is there any performance difference for nested document in mongodb query
– OhadR
Nov 11 at 10:25












Right, yes i want to confirm the answer was no difference?
– user1955934
Nov 11 at 10:40




Right, yes i want to confirm the answer was no difference?
– user1955934
Nov 11 at 10:40












1 Answer
1






active

oldest

votes

















up vote
1
down vote













If you store the extra data under the user, it enables faster reading and writing of entire user document.



If you the extra data under separate collections, it may enable faster access find/update of that data (depending on your indexes). MongoDB does enable indexing fields in arrays



My suggestion is to try and list your common data access use cases, create a test database with a LOT of mock data, then test performances using queries and aggregations to defer between the different storage modelling options.






share|improve this answer





















  • So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
    – user1955934
    Nov 11 at 10:19










  • @user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
    – Danny Varod
    Nov 12 at 11:41













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%2f53245820%2fmongodb-collection-structure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













If you store the extra data under the user, it enables faster reading and writing of entire user document.



If you the extra data under separate collections, it may enable faster access find/update of that data (depending on your indexes). MongoDB does enable indexing fields in arrays



My suggestion is to try and list your common data access use cases, create a test database with a LOT of mock data, then test performances using queries and aggregations to defer between the different storage modelling options.






share|improve this answer





















  • So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
    – user1955934
    Nov 11 at 10:19










  • @user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
    – Danny Varod
    Nov 12 at 11:41

















up vote
1
down vote













If you store the extra data under the user, it enables faster reading and writing of entire user document.



If you the extra data under separate collections, it may enable faster access find/update of that data (depending on your indexes). MongoDB does enable indexing fields in arrays



My suggestion is to try and list your common data access use cases, create a test database with a LOT of mock data, then test performances using queries and aggregations to defer between the different storage modelling options.






share|improve this answer





















  • So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
    – user1955934
    Nov 11 at 10:19










  • @user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
    – Danny Varod
    Nov 12 at 11:41















up vote
1
down vote










up vote
1
down vote









If you store the extra data under the user, it enables faster reading and writing of entire user document.



If you the extra data under separate collections, it may enable faster access find/update of that data (depending on your indexes). MongoDB does enable indexing fields in arrays



My suggestion is to try and list your common data access use cases, create a test database with a LOT of mock data, then test performances using queries and aggregations to defer between the different storage modelling options.






share|improve this answer












If you store the extra data under the user, it enables faster reading and writing of entire user document.



If you the extra data under separate collections, it may enable faster access find/update of that data (depending on your indexes). MongoDB does enable indexing fields in arrays



My suggestion is to try and list your common data access use cases, create a test database with a LOT of mock data, then test performances using queries and aggregations to defer between the different storage modelling options.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 10:14









Danny Varod

13.2k44787




13.2k44787












  • So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
    – user1955934
    Nov 11 at 10:19










  • @user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
    – Danny Varod
    Nov 12 at 11:41




















  • So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
    – user1955934
    Nov 11 at 10:19










  • @user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
    – Danny Varod
    Nov 12 at 11:41


















So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
– user1955934
Nov 11 at 10:19




So the first way i mentioned in my question, does not involve using separate collections nor array. I just structure it as a nested fields. Would this make queries/indexing for those fields that are nested?
– user1955934
Nov 11 at 10:19












@user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
– Danny Varod
Nov 12 at 11:41






@user1955934 Not sure what you meant in your comment, however, querying is possible on nested info, less straightforward though. Also, I highly recommend you check out the aggregation API. Also, don't forget the explain option on queries.
– Danny Varod
Nov 12 at 11:41




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245820%2fmongodb-collection-structure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)