Many-to-many relationship in NoSQL database

Rodrigo Palermo
3 min readOct 15, 2020

Using mongoose to create a many-to-many relationship

I am taking a course on programming analysis and development, and one of my tasks is to write something that I’ve learned and is useful for other students. One of my current programming projects is about online courses. The main goal is to connect student to teachers. A teacher sign in to the site and offers his courses to students. A student sign in to the site and can search, subscribe and unsubscribe to courses. Maybe you’ve already did a project like this one with a relational database (SQL). In order to study and use a non-relational database (NoSQL), here I am going to use the library mongoose, a mongoDB object modeling for node.js.

Precisely, in this post I’ll show you how to deal with a many-to-many relationship with mongoose. Let’s take, for example, a student that wants to subscribe to a course. Many-to-many in this case means a student can subscribe to many courses, and a course can be subscribed by many students. Concerning a relational database, a basic Entity-Relationship (ER) conceptual model is represented below.

ER conceptual model

A logical model showing the many-to-many (M:N) relationship is represented below.

ER logical model

The tables needed to be created in a SQL database include a ‘student’ table, a ‘course’ table and a third table that represents the many-to-many relationship, called, for example, ‘student_course’. This table must have primary keys columns (PK) with the ids of the student (student_id) and the course (course_id), as showed on the figure:

Tables — SQL database

Concerning a NoSQL database, and based on this article, we could accomplish the same many-to-many relationship by using the Reference Data Models (Normalization) approach.

Examples of ‘Course’ and ‘Student’ documents are shown below. Documents are analogous to rows of a a table in a SQL database, but is written in JSON, a JavaScript Object Notation. And collections of documents are analogous to tables.

The ‘Student’ model (actually in the code I use ‘User’ model with different roles that a user can assume, like ‘admin’, ‘student’ and ‘teacher’) and the ‘Course’ model must have a type used by mongoose, as showed on the following code. The teacher field in the Course schema refers to a one-to-many relationship. Note that the many-to-many relationship on Course schema is given by the the students field, that is represented by an array of the mongoose type referred to the User model. The same occurs in the User schema (courses field).

For instance, if a student wants to subscribe to a course, we must create a function that updates the course document with the student id and then, updates the student document with the course id. This is demonstrated below, on the partial code of the subscribe function.

The complete code files and a functional solution can be found in my github. I hope this post may help you.

--

--

Rodrigo Palermo

System Analyst and Developer / Cartographer Engineer