Displaying contents of sub-documents

Hello,

I’m quite new to Meteor and have been playing around with manipulating documents. I have been able to display top-level documents in my collection to the end-user, but was wondering if it’s at all possible to display sub-documents to the user. My document in Mongo looks like this:

        "_id" : "EyFYm5ixNezPafddr",
        "name" : "Profile",
        "Channels" : {
                "channel test" : {
                        "name" : "channel test",
                        "HighLimit" : 6.5,
                        "LowLimit" : 4.5,
                        "StopCount" : 10
                }
        },
        "steps" : {

        },
        "createdBy" : "psG7RnnD35J6uwceY"

My end goal is to have users create Channels, and as they create them they appear in a list and be editable. The piece of the puzzle I’m missing is actually displaying these channels. Any guidance would be greatly appreciated.

I think the document should look like this:

"_id" : "EyFYm5ixNezPafddr",
"name" : "Profile",
"channels" : [
  {
    "id": "some id",
    "name" : "channel test",
    "HighLimit" : 6.5,
    "LowLimit" : 4.5,
    "StopCount" : 10
  },
  // other channels 
],
"steps" : {

},
"createdBy" : "psG7RnnD35J6uwceY"

Read more Mongodb update sub document: https://docs.mongodb.com/manual/reference/operator/update/positional/

Thanks for the reply. I am a little confused on how this particular article would lead me on the right track. Do you mind explaining in a bit more detail?

You should read more about MeteorJS concept.
You get that list of channels from server by subscribing data publication or by using method.
Make form to user edit data
Update the data by calling method, the method (on the server) will update/insert the data.

@apegah what @minhna is trying to explain is to design your collection properly like what he had shown on his reply and then going to the tutorial from link

1 Like

What front end framework are you using - Blaze, React, Angular… ?

The front-end framework I’m using is Blaze.

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).

  1. 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.
  2. The more channel objects you have in each document, the harder it becomes to manage them - objects are not good for iterating over.
  3. 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 #each).

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.

2 Likes

Thank you for your reply. It’s definitely looking like a normalized data model is the approach I should take. Do you have any documentation that you recommend I look in to?

I had a quick look and there’s a number of articles around which discuss the pros and cons of normalised vs denormalised collections, but nothing I would point to as the definitive text on the subject.

I’ve been doing more research into the subject and have implemented a normalized data model into my project. Most of the topics you described as drawbacks to my approach were issues I had encountered and for the most part everything is going smoothly. I really appreciate all the help!

1 Like