Yes, Model Relationships are Possible in MongoDB — Lemme Show you how!

Emrich-Michael Perrier
3 min readSep 9, 2021

Alright so I think this will be the last blog post I decide to milk with the whole NodeJS/ExpressJS/MongoDB shin-dig. We have one last thing to touch on and that’s model relationships within mongoDB. For a refresher, it’s common for apps to use a conventional ORM DB for related data such as SQL. However, MongoDB has some advantages and I wanted to teach myself something new for this app I’m helping to make. MongoDB is a BSON Database that stores data in object documents similar to the JSON format. You can view my last blog post here to get up to speed on what’s goin on with my code at the moment. Let’s dive right into how to set up a one-to-many relationship in a MEN stack backend!

So we need to change 3 files to get this up and running. First we’re going to add references in the Schemas of both Comment and User. Keep in mind Comment will belong to User. You will add this to the Schema…

Look at the “comments” attribute part of the object starting on line 20. This is going to add a “comments” attribute to the User instance and it’s going to reference the comments that belong to that user. Additionally you’re going to have to add…

Likewise you’re going to need the reference of user in the comment schema as well. ignore the “question” part on line 21, that’s my attempt to setup another relationship with the Question model. Look at line 17 for the “user” attribute.

Now you’re almost done we just need to setup the controller to reference user upon the creation of a new comment. You’re POST/store/create action in controller is going to look like so…

You can see on line 18 that we have the user we will reference to when we instantiate a new comment. Upon sending the POST request the user id will be needed to make this relationship possible. Now we have one more thing to do, we need to include the user data when we pull from the api. This will be done on your GET routes. You will add…

.populate(‘model’); //to the end of finding the model, ie:const x = await Model.findById(req.params.id).populate('user');

In my code it will look like this…

Here in the show, we are finding a comment and then populating that instance with the user data we reference in the Schema with the user id included upon creation of the comment.

And that’s it!

My example test data now looks like this…

You can see here with each comment there is a user included and it shows the data of that user! Obviously sensitive data such as password and email wouldn’t be included in a deployable version but you can go further to filter out what you want and what you don’t in your UsersController.

That’s it!

I know this was a short one but it’s just that easy to implement something like this in mongoDB. I can totally agree that SQL makes this easier but I like not having to deal with table migrations and it’s nice that mongoDB is very scalable so I will have less of a headache later on! I hope this helped y’all! As always…

Happy Coding! :D

--

--

Emrich-Michael Perrier

Fulltime Frontend Developer, Anime nerd and lifter of heavy weight in competition