What is the best way to structure embedded docs in MongoDB?

I have a document layout like this:

Program = {
    '_id':ObjectId('4321...'),
    'Title':'The Title',
    'Episodes':[
              {
              'Ep_ID':'234122',  # this is unique
              'Title': 'Ep1',
              'Duration':45.2 },
              'Ep_ID':'342343' # unique
              'Title': 'Ep2',
              'Duration':32.3 }
                ]
            }

What I would like to do is at another embedded doc within each Episode, like this:

Program = {
        '_id':ObjectId('4321...'),
        'Title':'The Title',
        'Episodes':[
                {
                'Ep_ID':'234122'  # this is unique
                'Title': 'Ep1',
                'Duration':45.2,
                'FileAssets':[
                          { 'FileName':'video1.mov', 'FileSize':2348579234 },
                          { 'FileName':'video2.mov', 'FileSize':32343233 }
                             ]
                },
                {
                'Ep_ID':'342343' # unique
                'Title': 'Ep2',
                'Duration':32.3,
                'FileAssets':[
                          { 'FileName':'video1.mov', 'FileSize':12423773 },
                          { 'FileName':'video2.mov', 'FileSize':456322 }
                             ]
                }
                  ]
            }

However, I can't figure out how to add/mod/del a doc at that '3rd' level. Is it possible or even good design? I would dearly love to have all the data in one doc, but managing is starting to seem too complex.

The other thought I had was to use the unique values that happen to exist for the sub-docs as keys. I've thought about my sub-docs and they all have some kind of unique value. So I could do this:

Program = {
        '_id':ObjectId('4321...'),
        'Title':'The Title',
        'Ep_ID_234122':{episode data},
        'Ep_ID_342343':{episode data},
        'FileAsset_BigRaid_Video1.mov':{'Ep_ID_234122', + other file asset data},
        'FileAsset_BigRaid_video2.mov':{'Ep_ID_234122', + other file asset data}
        }

Any thoughts would be great!

Answers


Yes, you can definitely structure your data to have that kind of nesting. Whats more, you definitely shouldn't need to do anything special to accomplish it (at least using pymongo). just get your docs with an update cursor if you need to update existing documents, if that's your problem?

at least, for your first idea. your second idea for a schema is not at all a nice way to structure that data. for one, it'll be impossible for you to easily iterate over subsets of a Program document without doing string matching on the keys, and that will get expensive.

That said, I'm currently dealing with some major MongoDB performance issues, so I would probably recommend you keep your file assets in a separate collection. it will make it easier for you to scale later, if you plan for this data set to become large.


Need Your Help

Is there a way to check node and npm versions before a npm install?

javascript node.js npm npm-install

I want to only allow certain version of node and npm before the user can run his npm install on my module.

Variable assigned in dispatch block coming back null

ios iphone objective-c multithreading grand-central-dispatch

I'm trying to make a network call run in a background thread so it doesn't freeze my app while it's waiting.