Complete Intro to Databases

Updating MongoDB

Brian Holt

Brian Holt

SQLite Cloud
Complete Intro to Databases

Check out a free preview of the full Complete Intro to Databases course

The "Updating MongoDB" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to update an object within MongoDB by first querying the object that needs to be modified, and then modifying that object. The concept of upsert is also discussed in this segment, which is to find an object in a database, and updating it, or if that object does not exist creating it.

Preview
Close

Transcript from the "Updating MongoDB" Lesson

[00:00:00]
>> So we're gonna get into updating Mongo DB. So, so far, we've just must have been talking about reading and writing to Mongo DB. We're gonna be talking more about now, how to do updates, deletes, those kinds of things. So the first, like we've already talked about, insert one and insert many, right?

[00:00:15]
Insert one is, I wanna insert one thing into my database or my collection. Insert many is, I have an array of documents, I would like to insert all of those into my collection. You'll also notice that there is a function called insert, right? It's not insert one and so many, but just insert, this is actually the old way of doing it.

[00:00:33]
I would recommend to not use insert anymore. I think it's also deprecated according to MongoDB. It would intelligently decide if you're trying to insert one or if you're trying to insert many, depending on what you gave it. But what they found is that, people were just messing things up and accidentally inserting too many or accidentally and only inserting one thing.

[00:00:53]
Normally, that way, they would try and be to insert many objects, and they would actually end up inserting one very, very large document. So they just recommend, tell MongoDB up front you're trying to insert many or insert one. And this is true of all of the methods when we talk about, there's a delete one, delete many, delete, just use the delete one and the delete many, and same thing with update as well.

[00:01:17]
Do the update, many or update one. So I'm not gonna show you again, insert one and insert many, we already did that one. So we're gonna move on to updates. So the first one that we're gonna do here is, we're gonna update one. So what I'm gonna do now is, I'm gonna say db.pets.update1.

[00:01:39]
So this is gonna work just like find one, where it's going to find that the first one that it encounters, I think it's by create date. It's going to update that one, it's not gonna update anything else. So here, I'm gonna say, update one. And the first thing that you're gonna give it is a query object.

[00:02:00]
This is the exact same object that you provide to find or find one or find many, right? Or there's no find many, it's just find. So I'm gonna say, find one that's a dog, that's named Luna. And you don't have to put quotes there, those quotes are optional, cuz it's JavaScript, right?

[00:02:25]
Name, Luna, breed, Haveanease. So we're gonna find my Luna, okay? And then the second object that you can provide to, is you're basically gonna tell MongoDB, how do you want to update this? We wanna set her owner to me, Brian Holt, dollar signs set. So this is going to be basically an object that's gonna do like an object merge, or what is object at assign, if you're familiar with JavaScript, that's how this is gonna work.

[00:03:00]
So I'm gonna provide this in another object, and I'm gonna give it an owner of Brian Holt, Okay? So let's just kind of break this down, update one, you're gonna provide it a query object and an update object. The first one is going to find the record that you're trying to, or the document that you're trying to update.

[00:03:29]
The second one is going to tell it how to update it. So if I run this, did I not include enough, that's what I did wrong. Hold on, I put too many curly braces. This seems to happen to me quite a bit, I'm just gonna copy and paste this one.

[00:03:56]
I need to do that again. There we go, so it'll give you acknowledged true, this is like, this was successful, this actually did happen. It matched one thing which is good, cuz that's what I asked for, update one and I modified one thing. So if I ran this again, you would notice that it said, it matched one, but it didn't modify anything, because that was already the way that it was, right?

[00:04:25]
So now, if I change this from, if I say db.pets.find, and I searched by owner, Brian Holt. You notice, it's only gonna return one. So a couple things that I've demonstrated here, one, we just showed you how to do update one, right? So that's gonna be updating this one to be Brian Holt.

[00:04:50]
But also notice that, no other pet object or pet document in our collection has an owner. I just made that one up on the flier, that piece of schema, right? So that's what's really cool about MongoDB, is like, if you wanna change the schema of your collection, just do it.

[00:05:08]
There's no pomp and circumstance to do anything about this. You just go, and you can change things on the fly. It's one of the superpowers of MongoDB, it's also one of its downfalls, cuz it's really easy to mess this up, right? Because it's not going to challenge you, the guard rails are of, you can do whatever you want.

[00:05:28]
So it's kind of a double edged sword there. So let's do an update many. We're gonna do db.pets.updateMany. Let's say it's every dog's birthday today, it's dog birthday day, let's do that. So the first thing is we're gonna do is, we're going to do an updateMany. We're gonna give it a query object.

[00:05:52]
So we're gonna find every dog type, dog, and then we're gonna give it a query object. And we're gonna say, when to use a special operator for updates. And we're going increment all of their ages by one, right? Because some of them will be af age 1, some will be age 2, some will be age 3, and we wanted to just add 1 to all of those.

[00:06:17]
So we say, $inc for increment And then we're gonna find age, and we're going to want to increase it by 1. You can increase it by 2, right? If you wanted to So now, if we run that, you can see that we matched 2501 documents in the collection, and we've modified all of those.

[00:06:43]
So actually, if it's find again that dog, you can see here, Luna's age was age eight here, and now, you can see Luna's age is age nine, because we incremented all of their ages by 1, make sense? There's a bunch of those. There's, I put a link in here as well, you could just take a look here.

[00:07:07]
Increment, rename, you can rename fields. So for example, if you're trying to change names to be Title, I don't know, something like that. You can do this renamed, names the keys for all these, you can unset, so you can just delete something off of an object. You can insert the current date, all these things are available to you.

[00:07:40]
So we talked about update, specifically using this set, yeah, I can see here, this particular set operator to change an object. So we were just changing and leaving everything else the same. There's something as well called replace. So you can replace one and replace many. And instead of providing here a set object, you provide the whole object and it will replace everything.

[00:08:08]
That makes sense? So, yeah, be aware that you can use update or you can use replace. So let's talk about upsert, which is a ridiculous word, but it is one that you'll hear a lot in talking about databases. Upsert is the idea, is like, if you find something, update it.

[00:08:31]
If it's not there, insert it, does that make sense? So it's kind of this atomic transaction, or this atomic query that allows you to change something if it exists. If not, then go ahead and update it. So let's go ahead and run one of those really quick. So we're gonna say, DB.pets.update1.

[00:08:59]
And let's say we wanna put in here, Sarah dog. I don't know if you've ever seen Sarah dog, but it's adorable. The dog's name is Sudo, so you can tell the dog to sudo sit, which is just the funniest joke that never gets old to me, all right?

[00:09:15]
So type dog name, Sudo. You'll notice that we don't have a dog named Sudo in our database right now. And the breed is a wheaten, which I had to go look up, which are adorable dogs by the way, okay? So that's the thing that we're gonna query for, that query will not match anything on a database.

[00:09:40]
So what we're gonna do here is, we're gonna give it a set object. So dollar signs set, and because it's an upsert, we wanna provide it the whole object, because if nothing matches that dog, then it's going to take that set object and make that the new object.

[00:09:58]
So you have to provide it with everything else. Also will be lacking that in the new object. So type dog, name, Sudo. Breed, Wheaton, age, 5. I don't actually know how old Sudo is, but I just took a guess. Index, we'll just give it a random index. So I'm gonna give it 20,000 and the owner is Sarah Drasner.

[00:10:37]
If you don't know Sarah, she's another instructor in Frontend Masters, and has amazing courses on design and view and lots of other cool stuff. Okay, so that's our set object. Then the last thing is, we're gonna give it one more parameter to this update one, this is like a configuration ones.

[00:10:58]
We can provide different sorts of configurations to this query. We're just gonna give it an upsert:true. That basically says, if you don't find it, insert it. I forgot a curly brace here. So I'm just gonna copy and paste this from my notes. Promise you it's the same thing though.

[00:11:27]
So we're querying for this, it's not gonna find it. We're gonna try and set it to this. So if this did exist, this would get updated, because it's not going to exist, it's going to get inserted. So you can see here, match 0, modified 0, upserted ID. This means it actually did get inserted into our collection.

[00:11:48]
So now, if we just said DB.pets.findpets, find name, Sudo, we would find, Find one. Our good friend Sudo the dog.

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