Introduction to MongoDB

Add Similar Posts Solution

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

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

The "Add Similar Posts 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 final query that adds posts to existing posts, instead of wiping them away.


Transcript from the "Add Similar Posts Solution" Lesson

>> Scott Moss: And then the last one is add related post, so you have to do a lot of work to get this one to work. Did anyone get this last one to work?
>> Scott Moss: Yeah, it's a lot of work. Okay, so this last one if you look at the test, add related post, should not override related posts that are there.

So I mentioned something in the, to do that, you had to add a field for related posts, right? But there was no test for that. So when you got to this point, this is where you would have to have added in order to get this to work. So that was the first step, you had to add that.

And that's why I put that in the readme. And then after you add that, the next thing you gotta do is actually figure out how to do a mutation against that array without erasing everything. So we gonna walk through that step by step. So the first thing is head over the post and if you look at the test.

>> Scott Moss: Go back to the test.
>> Scott Moss: Clear that expect, it looks like it's expecting the post to have an array called similarPosts whose value Types is like ObjectId. And it's also the same thing that I've put in the Readme, you have to make another field. It's gonna be many-to-many, it's gonna be an array.

So you would have to go do that, so we're gonna go ahead and do that here.
>> Scott Moss: Post, so now I'm gonna go down to the post field and I'm gonna add similarPosts. And it's gonna be an array type and we looked at array types before. The only difference is it's belonging to an association.

An association is itself, it's a post. So what we're gonna do is we're gonna say it's type is mongoose.Sxchma.Types.ObjectID, there we're going to say the ref is going to be whatever the name of this collection is, which is post, so we'll say post. Here we go. So now it's going to be an array of IDs that reference the post correction.

So that's step one, I will over to expect file and then now we can actually fill this function where its addSimilarPosts. So it's gonna give you a postId and it's gonna give you an array of similarpostIds to update the given the found POST by the Id with. So that means you don't want to blow out the array of similar posts that are already there for the given post, you want to add to that array.

So you don't want to set, you want to update. And by default if you just wrote a regular query, you might set it and overwrite it and the test is gonna test for that. So if you got to this point, you might have overwritten your array and like, I don't know what to do here, you gotta update the array.

So the way you wanna do that is we wanna do a return Post.find. Let's do a find by Id cuz we get an Id, so that makes sense. And also you wanna update, so findByIdAndUpdate. So we do update here and we wanna find by this Id. And then for the update, this is where it gets kinda tricky.

What we wanna do is, when your operating with arrays in Mongo, and you wanna do CRUD on them, you want to use the push helper property from Mongoose, it's actually Mongo, but Mongoose uses it as well to push things into that array. So I want this $push. So I'm gonna say $push, and then from here what you do is you have to say what field do you wanna push on.

In this case, I wanna push on the similarPost field. And from here what you normally would do is you just put a value here. So I just put like an ID, but the problem is, I have an array of values and I want to push, I want to put each one of those inside of this similar posts array.

Each one of them, so if I just put the array here like this, similar post it might discover write what is there already and then test already put some in there so you will completely override that. So instead you wanna update that, so what you do is you'll use the $each method here and then you can say similarPosts.

So basically what this is saying is I want to add a push command on the field called similarPost and I want to push in each item inside this array called similarPosts. So if there's three items in this array, they're all gonna get pushed into this array.
>> Speaker 2: What is the push command?

What kind of command is this?
>> Scott Moss: This is a mongo native command, it's how you operate on arrays with mongo so if you want to add items to an array, you would have these pushed. Just like the same way you would do it in JavaScript, you'd have to do dot push, it's the same way, yeah.

So yeah, dot push is a native JavaScript thing with arrays, so you're pushing something to the beginning of an array, yeah.
>> Scott Moss: And then we'll .exact and then because this is an update, I'm gonna test probably I'm gonna fill this and put a new true, right last time so make sure we get updated document not the one before you got updated.

>> Scott Moss: So we'll do that and we go back and, sorry postId Post ID, there we go. So we'll do that and all the tests pass.

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