Reputation: 103
I'm new to Mongo and have been trying to suss out the best way to structure part of my database. Imagine a document building program in which a user composes a text document by select 'paragraphs' from a pre-defined template. My question is just about how to store the template structure; when a user saves their text document it's completely separate and uncomplex.
A full 'template' could look as follows (simplified). :
{
description: 'Template 1',
someOtherField: '',
. . .
sections: [
{
description: 'Section 1',
paragraphs: [
{
description: 'Paragraph A',
subparagraphs : [
{
text: 'This is some text',
optionalTexts: [
{
text: 'This is optional'
},
. . more . .
]
}
. . more . .
]
}
. . more . .
]
}
}
As you can see they are fairly nested and can potentially encompass a lot of data.
I'd be running queries to achieve things like "List the sections belonging to template 56", "Give me the paragraph objects for section 1234"; as well as administrative tasks on individual items, "Update the text of subparagraph 5678 to ..", "Create a new paragraph for section 7568..".
I currently have two collections:
Is this a more sensible approach than having one collection with even deeper nested document?
If there isn't an "answer" to my question, some good resources would be greatly appreciated.
Thanks
Upvotes: 2
Views: 3753
Reputation: 10417
See the answer to this question. In the current mongo , you cannot update deeply nested sub-documents in an array unless you specifically know the index of the sub-document. If you are planning on updating the sub-documents, you should probably break them into separate collections.
You could put foreign keys in the sub docs and search on them, but beware of course that Mongo has no concept of joins, so depending on how you are going to need the data later, this could bite you in the a**.
As far as I know it is an open issue in Mongo that is kind of being addressed by the developers. Here is the open ticket
Upvotes: 1