1

I am learning Meteor & MongoDB. I wanted to know, how to relation handle in mongo/meteor. I have 2 collections. Teachers and Subjects. One Teacher can have multiple subjects. How to design this relationship using mongo meteor?

tao
  • 82,996
  • 16
  • 114
  • 150
Al Imran Hossain
  • 119
  • 1
  • 2
  • 12
  • 1
    Does this answer your question? [Meteor.publish: publish collection which depends on other collection](https://stackoverflow.com/questions/26398952/meteor-publish-publish-collection-which-depends-on-other-collection) – Christian Fritz Sep 05 '20 at 17:47

1 Answers1

1

You can do it with a relational technique, it is quite common for people to create 2 collections, as you have done. You can add a teacherId to the subjects collection, so that you can have many subjects per teacher.

If you want to model a many-to-many relationship, the relational model wants to use a linkage entity, ie a small table that refers to both teacher and subject. A way to do this in mongo is for one of the collections to have an array of references, so for example subjects can contain a 'teacherIds` field, which is an array of teacher id's.

If the subjects don't really have any additional data, you can just create an array of subjects within teacher. These can be a simple string, or an object if more data is required.

Mikkel
  • 7,693
  • 3
  • 17
  • 31