Introduction to MongoDB

Associations Exercise

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

Check out a free preview of the full Introduction to MongoDB course

The "Associations Exercise" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott instructs students to complete an exercise with the goal of creating relationships between models. The models will then be utilized to create more advanced queries than the previous exercise.

Preview
Close

Transcript from the "Associations Exercise" Lesson

[00:00:00]
>> Scott Moss: What I wanna do now is walk through the next exercise, and what you're gonna be doing, and how you can get started. So the next exercise is gonna be in the queries folder, and there's gonna be a couple of things there. There's gonna be an author.js, a post.js, and a queries.js.

[00:00:14]
So if we read the README,
>> Scott Moss: Preview, in this exercise you will have to add relationships between models. And then you will have to use those models to create slightly more advanced queries than in the last exercise. So exactly what I just showed you. Building relationships between these two models, and then writing some slightly more advanced queries.

[00:00:36]
So check out to the start branch if you haven't already, and refer to this README for how to run your tests. It is a new test command, so it's not the same test command you ran last time. The only thing that's changed is the path. Again, this is so it filters out the tests that aren't necessary for this exercise, so you only see tests for this exercise.

[00:00:54]
If you run npm test, or Jest, you'll just run tests for everything, and the future exercises will just fail. So this is just to help you, scope you for this. So basically, yeah, the things you're gonna be working on are the post models should have a one-to-one author field that points to the author collection.

[00:01:11]
So what that means is, this post field should have an author field here that points to the author collection, basically. And there should be one author. So a post should have one author, basically. What else it's saying is,
>> Scott Moss: The post model should also have a one-to-many field called similarPost field that points to posts.

[00:01:35]
So that was really confusing, but basically what that means is there's another field inside the post object that's called similarPost, and it's an array of posts. It points to itself. Yeah, and I know we didn't talk about that, so you might be thinking, what does that mean? Yeah, I promise you it's no different, that's the trick.

[00:01:55]
[LAUGH] So that's like a one-to-many, right? So this one post can relate to many other posts, so you're getting some type of graph type things. If you're on a blog post at the bottom it's like, here's some similar posts, that's what that's for.
>> Scott Moss: So you got that.

[00:02:10]
What else you'll be doing is, you're gonna get all the post model tests to pass. So this test associated with those schema changes. And then you need to get all the query tests to pass. So if you go look at the queries, it's very much like you did last time.

[00:02:23]
There's some empty functions in here that take some arguments with some very descriptive tests, you gotta get the tests to pass. I only showed you about 60%, maybe 70% of what you need to get these to work. As you get down the list, they're gonna get more difficult.

[00:02:38]
And yeah, I feel pretty confident that you're gonna be able to figure it out, cuz you did it last time. So yeah, you've just gotta get all these to pass. And again, you can check out the tests. They're in the local underscore underscore test folder, in that folder.

[00:02:51]
For instance, queries.spec has very detailed tests, and I'll just run the tests right now so you can see.
>> Scott Moss: So it looks like there's two suites. Eight failed, which is exactly what we were expecting. And for instance the queries, you can see it's testing a postByTitle, get a post by title.

[00:03:19]
The next one is getting a post by an author. The next one is finding all posts in between two content lengths. The next one is find post by ID, and populates everything on that post object. There's another one that says, only selects fields given. Didn't really talk about that, but I think you can figure it out.

[00:03:38]
And this last one is, should be able to add relationships to a post. So we only talked about querying for them using arrays, now you're gonna have to add for them. And I can give you a little tip in a minute what that looks like, but these two are gonna be pretty challenging.

[00:03:53]
The rest of these, we literally just did all of these. And then below are just gonna be the tests for the actual schema changes.
>> Speaker 2: That second to last one is basically GraphQL, am I right?
>> Scott Moss: [LAUGH] This one?
>> Speaker 2: Like only give me the fields that I'm asking for?

[00:04:11]
>> Scott Moss: It works very well with GraphQL, but yeah, I use this with GraphQL all the time. The answer is in the name, I'll tell you that, select. But yeah, this allows you to do what they call projection, right? You can project. You can tell the database to only project these fields.

[00:04:29]
A post might have all these fields, but you only want these fields. So how do you tell the database to only give you these fields, versus getting back all the fields and then using JavaScript to only select the fields? That's very wasteful, you can have the database do that for you.

[00:04:41]
So that's what this is. Ask the database for only the minimum amount of thing that you need, so that's what this does. So yeah, it does work very well with GraphQL. You could take a GraphQL query that has these fields, feed it straight to this, and only get those fields.

[00:04:54]
And that's just perfect.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now