It looks like you’re intending to use the
Channels object for a group of channels?
That’s an OK approach, but has a number of constraints (which may not matter to you).
- Each channel object identifier will be unique. That also means that there is potential for accidentally overwriting an old channel object with a new one having the same identifier. If that’s a problem for you, you will have to protect against it, which can be difficult to do atomically in code.
- The more channel objects you have in each document, the harder it becomes to manage them - objects are not good for iterating over.
- You need to know the object identifiers before you can reference them, which makes the displaying of arbitrary objects a challenge.
@minhna has suggested using an array of objects instead of your object of sub-objects. I have to say I’m not wild about that approach either. Of all the questions about MongoDB that we get in the forums, many are about how to manage arrays with Mongo queries. However, it’s a valid approach - arrays are naturally iterable (in Blaze that means an array of objects can be iterated over with
I’d be inclined to have a channels collection with one channel object per document and references via “foreign keys” to other collections where necessary. In other words use a more relational normalised model. There are issues with that approach too, but the advantages of using the database engine for managing duplicates and atomicity etc, outweigh the disadvantages for me.
If you definitely want to continue with a sub-document approach, there are ways to do that using a helper to present the sub-documents as an array, which can then be iterated over.