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

The "Associations Solution" 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 live codes the solution to the exercise.

Preview
Close

Transcript from the "Associations Solution" Lesson

[00:00:00]
>> Scott Moss: All right, welcome back, hope everyone had enough time to get those exercises done. Like I said, the last couple ones have been challenging. And you definitely will either have to have a really good intuition and a really good guess. Or had to do a little bit of exploring in the documentation after you figured out what the right idea was after completing some of the previous ones.

[00:00:19]
So I'm just gonna walk through some of those or all of those solutions. So let's go ahead and do that. So we head over to the queries folder, we have, I'm gonna do the side by side thing, so I'm gonna start with the posts. So open that up and then I open up post js, so, here, then post js.

[00:00:41]
Okay, so we start with these tests. This test says author is required and must be like an id type. So if I go look at the post schema, there's no author here, and it's definitely not an id type, so I need to do that. So I'm gonna to make an author.

[00:00:58]
>> Scott Moss: I'm going to give it a type of mongoose.Schema.Types.ObjectId, I'm gonna make it required.
>> Scott Moss: And there we go, so that should be all that this is testing for. So if we run the test, we'll see if this passed or not. And if not, we'll have to figure out what the test is asking for.

[00:01:23]
I got to go back into my program. There we go. So the testing command for this is going to be yarn tests exercises/queries/__test__/.
>> Scott Moss: Cool, so it looks like I got one of them passing. So if I go look at that it was author is required and ID type.

[00:01:53]
So that satisfied that one and let's put this on watch. There you go. So we'll go to the next one. Which is author must reference author collection so I can put the name ref as a tip of what to do. So if you go back to the author field that you made it needs to reference an author collection, so to do that you go back to the author field and you put ref.

[00:02:17]
And then you put the name of the collection, and the way you would get the name is if I went to go look at the author file, author.js. The name of the collection's gonna be right here. In this case it's just author. So I'm gonna copy that. I'm gonna go back here, and I'm gonna say 'author'.

[00:02:34]
And that's gonna be the name of the collection that it's referencing. So if I look here, it looks like I got two tests passing, so I scroll up to verify, and yep author must ref author collection. So that one's good. Any questions on the post model? Is everyone able to get those to pass?

[00:02:55]
>> Scott Moss: Okay. Awesome, so now we're going to move over to the queries and I'll open those up.
>> Scott Moss: Yeah, so these were kinda fun. Maybe kind of annoying depending on how you thought about it. But, I think it made you think about little bit. So post by title, what you can do here is, I mean there's no wrong way, there's no one way to really do this.

[00:03:27]
There's so many ways you can do a lot of these queries, so I'm just gonna just do it the way I've already solved it. But so for the post by title, it's pretty simple. We just wanna do a quick filter on the query parameters that we're passing in, right?

[00:03:40]
So you can see that is expecting, whoops, let me get the test of, over here on this one. There we go. So post by title. Get post by title. So it looks like it's expecting one post and not many posts. So I'm gonna roll with that and if it expects too many posts, then I'll change the query.

[00:04:02]
So we want to be able to just say Post.findOne. And then I'm just gonna pass in a title as the only parameter, and I'll do a .exec just to make sure. So we're gonna run that, looks like we got three tests passing. Scroll up to verify and get postByTitle, that was it, so we passed that one.

[00:04:25]
Anybody do that one differently?
>> Scott Moss: No, that was pretty straightforward. Posts for authors. This one looks like it's plural so down here, text getPost by an author, okay? And this, it looks like it's taking an author ID. So I'll just return Post.findOne by the field author, and its value is authorId.

[00:04:52]
Remember author is just an ID. Only when we call it partly is an object. So we can just mash our IDs here. So this should find that one.
>> Speaker 2: It was post polarized.
>> Scott Moss: Yep, post. There we go. Sorry about that. So it's going to be Post.find so it we'll look at all of them.

[00:05:08]
So if we go look, four passed and get post by author works. So that one works. We'll go to the next one which looks like it's postByContentLength. This one gets a little more challenging. Who completed this one? Got three, okay. I'm very curious to how you would have done it.

[00:05:29]
So I'm gonna walk through mine, and then I'm gonna ask how you might have done it. So postByContentLength, if we look at the test it tests find post in between content lengths. So it's gonna give you a max and a min and you should be able to get back any posts who has content length in between that maximum and the minimum.

[00:05:49]
So the way I can do that is if I just return Post.find [SOUND] and then what I want to do is use some of the operators like greater than and less than. And use these arguments, the maxContentLength and minContentLength, cuz if I go look at it post file,

[00:06:11]
>> Scott Moss: The model itself, you could see that there's a contentLength filled on it whose value is a number, and it's required. So it always has a contentLength, so that's the one we wanna query on. We wanna do the query on that. If you try to do querying on the content itself, and try to do greater than or less than on that, it won't work because those aren't numbers.

[00:06:29]
Those are strings. So you can't do, won't do the greater than the equal, the greater than, the less than on a string. Although JavaScript will do it. So that's probably why, if you've done it that way, you probably thought that was gonna work. But Mongo won't do it that way.

[00:06:41]
It's only gonna do that on numbers. So heading back to the test.
>> Scott Moss: So the way I would do this is, one I want to query on the content length field. And then I want dollar less than, so anything less than the maxContentLength because that is the upper bound.

[00:07:04]
And then anything greater than the minContentLength, and it's not exclusive so I'm not gonna put less than or equals to, which does exist in Margo, or greater than or equals to. I'm not gonna make it exclusive. I'm gonna cut off there cuz that's what it seems like the test is doing.

[00:07:19]
And if you did put E there on either one of them, you probably would have failed it. And actually like no, not those. So I think I hard coded it to the exact lengths than needed to be there. Yeah. So that was testing that. So once you got that, you need to do .exact and we'll see if that passes or not.

[00:07:38]
So let's think that I got five that passed so let's check it out. And find post in between ContentLength pass. So that's pass this one. Yes?
>> Speaker 2: You say how you did the ors. [COUGH] If it was or, what would you do?
>> Scott Moss: If it was or?
>> Speaker 2: Yeah.

[00:07:51]
>> Scott Moss: If this was or, then the way you would do this is you would say or looks like, takes an array.
>> Speaker 2: Okay.
>> Scott Moss: So, you do or like that, yep.
>> Scott Moss: Cool, so the next one is fullPostById. So this one you really probably have to fail the test a couple times to really understand what it's doing.

[00:08:17]
Maybe you even have to look at the test to understand what it's doing. But basically, if you read the test, assertion here is find post by ID and populates everything. That's basically it. So how do you know what to populate? Well, if you go look at a post model, you can see that we need to observe what on here needs to be populated or not.

[00:08:39]
So, so far, a post just has this author so we have basically just this author to populate and that's it. So that's the only thing we have to populate for this test to work. So we will go back to queries, exec, and fullPostById so I will say return post.

[00:09:04]
>> Scott Moss: .findById, pass in the ID. And I'm gonna use a populate, cuz that's how we populate stuff. And it says it in the hint right here, find by ID and populates everything. So the thing that I wanna populate is gonna be the author. And then I'm just gonna exec.

[00:09:25]
>> Scott Moss: So I got six passing now. So findsPostById and populates everything.
>> Scott Moss: So the next one is selective posts by latest, so if you scroll out on that one. Only it selects fields given. So what this is saying is, it's going to pass you, where is it at?

[00:09:55]
It selects
>> Scott Moss: SelectivePostsByLatest, this one is, excuse me. By allPostsSlim there we go yeah. So the names messed up, so yeah allPostsSlim so this one is gonna give you fields that it's gonna ask you to select from the database. So you could have just cheated this test by just getting everything to the database and selecting over JavaScript then you go to pass the test.

[00:10:18]
But you should let the database handle it. So what you wanna do here is, looks like it wants all posts, so I'll just say post. Oops, posts., my God, it's all autocomplete, .find, all, so I'll just do a star there. And then I wanna do a .select, and select is the method you wanna use here.

[00:10:41]
And this object right here, fieldsToSelect is gonna be an object that looks something like this. It's gonna have a field on it like, name: -1. That means don't select name. So it's gonna have a whole bunch of fields that belong to the post with a number on them.

[00:10:57]
And you can pass that straight to select.
>> Scott Moss: And then do your .exec. So we'll run that.
>> Scott Moss: And looks like I got seven tests to pass, so we scroll up. That one passed to only selects fields given.

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