Meteor and embedded documents


#1

Hey,

When it comes to meteor I’ve seen people create different collections instead of embedded documents (even though in most cases embedding would be considered more efficient in the mongoDb context)

So my question is if I have a collection like the one below does it make more sense to put classes , sections, questions and videos in separate collections and then join them?

{
    "classId": "01",
    "Sections": [
        {
            "sectionId": "s1",
            "Videos": [
                "video1",
                "video2"
            ],
            "questions": [
                {
                    "questionId": "q1",
                    "question": "Some text",
                    "answer": "some text"
                },
                {
                    "questionId": "q2",
                    "question": "Some text",
                    "answer": "some text"
                }
            ]
        }
    ]
}

Thank you
Shibbn


#2

One pattern I see fairly commonly (and prefer personally) is using publish composite and collection helpers to manage these relationships.

Yeah, it’s slower, but it’s more reliable.

// reywood:publish-composite
// get a class and the sections
Meteor.publishComposite('class', function (classId) {
  check(classId, String);
  return {
    find: function () {
      return Classes.find({ _id: classId });
    },
    children: [{
      find: function (class) {
        return Sections.find({
          _id: { $in: _.pluck(class.sections, 'sectionId') }
        });
      },
    // etc etc
    }]
  }
});

With helpers on your collection like this:

Classes.helpers({
  sections: function () {
    return Sections.find({
      _id: { $in: _.pluck(this.sections, 'sectionId') }
    });
  }
});

#3

Hey thanks for the quick reply

So that would mean setting up a collection for each entity instead of embedding anything? Basically similar to a relational database?


#4

yeah this method is more similar to a relational database. You can set it up more NoSQL like, but you’ll have to use a lot of collection hooks to maintain integrity of data