0

I'm designing out a mongodb schema that will be used to generate a dynamic user interface used to collect data. The basic idea is to have a "Template" document contain a definition of all the fields in the user interface. Within that same "Template" document, there is a schema that defines the layout of the interface. The interface has a nested structure of sections, subsections, field groups, and fields; this hierarchy corresponds to logical groupings of related data. To manage user access, we also need to define "Views" within the "Template" document as well. A "View" defines what sections, subsections, field groups, and fields can be visible. We would only need to retrieve one View at a time so we want to filter, and project the portions of the "Template" visible for a specific view. So far this is our schema:

 {
         _id: ""
         "TemplateName": "Template 1"
         "sections": [{
                    "name": "Details",
                    "sort": "1",
                    "views": ["View1"],
                    "subSections": [{
                        "name": "General",
                        "sort": "1",
                        "views": ["View1"],
                        "fieldGroups": [{
                            "name": "Group 1",
                            "sort": "1",
                            "fields": [{
                                "fieldId": "59d662a6c153f67518f98880",
                                "sort": "1",
                                "views":[{
                                    "name":"View1",
                                    "access":"read"
                                }]
                            },
                            {
                                "fieldId": "59d662a6c153f67518f98881",
                                "sort": "1",
                                "views":[{
                                    "name":"View1",
                                    "access":"read"
                                }]
                            }]
                        }]
                    }]                
                }]
        }

Now this design works great since we limit the amount of duplication by embedding the names of views in a particular sub document that is included in that view. For example the "Details" section is visible in "View 1". The problem is how to query this document efficiently based on view name and project only the specific sections, subsections, fieldGroups, and fields that have the view name inside of their views array property. I've only been able to do this by first unwinding, then using the match operator, but this is messy since we have to unwind at progressively deeper levels to get the full structure.

Advice and recommendations are appreciated here.

user95488
  • 129
  • 2
  • 11
  • It's really not a great idea to nest arrays. Actual support for updating "atomically" and efficiently with safety does not yet exist in MongoDB production releases, and even with that inclusion it certainly does not make the actual "query" or "inspection" of such nesting any easier. So what you "perceive" as a "benefit of nesting" is usually best handled in actuality by placing the "nesting key names" as "attributes" on a singular "flat" array instead. This is a lot easier and far more useful for queries as well as facilitating atomic updates in existing production versions. – Neil Lunn Nov 11 '17 at 02:35
  • For more general detail see [Find in Double Nested Array MongoDB](https://stackoverflow.com/a/29072062/2313887) and [Updating a Nested Array with MongoDB](https://stackoverflow.com/a/23577266/2313887) which both describe available methods, the pitfalls of "nested arrays" in design as well as different approaches to design you really should consider. – Neil Lunn Nov 11 '17 at 02:38
  • Thanks, very much appreciated – user95488 Nov 11 '17 at 03:33

0 Answers0