MongoDB Crash Course

Dev Ittycheria & MongoDB
1 follower
2 Video Views·Jun 11, 2026
MongoDB is a complex NoSQL database with a ton of commands and intricacies. In this video I will be breaking down all the most important commands and showing you that MongoDB is actually a lot simpler to get started with than it looks. This video will cover all the Create, Read, Update, and Delete commands that you need to know.
AI Generated·May contain errors
If you need a database for your project, then MongoDB is a great choice, but it can be difficult
to learn at first. So in this video, I'm going to break down every important concept in MongoDB
so you can become an expert by the end of the video. And we're going to cover everything from
the basics of create, read, update, delete, all the way to more advanced queries and advanced
updates, so make sure you stick around to the end of the video. Welcome back to WebDev Simplified.
My name is Kyle, and my job is to simplify the web for you so you can start building your dream
project sooner. And in this video, we're going to be doing a MongoDB crash course. Now to follow
along in this video, you're first going to need to install MongoDB. I'm going to have links to
these pages in the description below, but essentially go to this page, scroll down,
find your operating system, and then just install the community edition of MongoDB.
Walk through the install step, and that'll be all you need to do. And then once you have MongoDB
installed, you're going to need to install MongoSH. This is the shell that you can work with
to actually access and read database queries. So just come down here to your operating system,
click on it, and then follow the steps down below. It's pretty straightforward,
and once you have that done, you can open up a terminal, which I just have here,
and you can type in the command MongoSH. That's going to open up that shell, and as you can see,
it says we're using MongoDB version 5. We have version 1 of MongoSH. That's because this version
5 of MongoDB and MongoSH are very new. They came out just a few weeks ago, so this is going to be
the most up-to-date information you can find. Now the first set of commands I want to talk about are
just basic commands that allow you to actually use the MongoSH terminal and be able to create
and view different databases. And first, you'll see that we have the word test written here.
That's essentially the name of the database that we're currently using. This is just the default
database you get started in. It's a test database that has no data, and it doesn't really exist yet
until you create data. Now in order to view your databases, you need to type in show DBS,
and DBS just stands for databases. And you'll see here we have an admin, an appdb, a config,
and a local. When you type this in, you should see just admin, config, and local, and you won't see
this appdb. That's because I created this appdb already, and it has some data already in it.
Now if you want to be able to access and use a database, just type in use followed by the name
of the database you want to use. So in our example, we could say appdb to use the appdb database,
or we could create a brand new name right here if we wanted to, and that's going to create a
new database and put us inside of that. So for our case, let's just jump into this appdb database now
since that exists. I click use, you can see it says switch to that database, and now it says
appdb instead of test. Now in order to view what collections we have in that database, we can just
type in show collections. And in MongoDB, there's essentially two different things. You have
databases, which are just like databases in SQL, and then you have collections. And collections
are like tables inside of a database if you're familiar with SQL. It's just where you store your
data, and you can call them whatever you want. So we have a user's collection inside of this
database. Now obviously we don't really want to use this database because it's already pre-built,
so I'm going to show you how you can actually delete an entire database. And to do that,
you just type in db.dropDatabase() and call it like it's a function. The nice thing about
MongoDB is if you're familiar with JavaScript, a lot of the syntax in MongoDB is very similar
to JavaScript. You're calling functions and passing them parameters. So if we type this,
you can see it says ok1, and it dropped appdb. And now if we click show dbs here and type that in,
you can see that that appdb no longer exists. And also if we want to clear our screen, we can type
in cls. It's going to clear our screen completely and put us back at the top. This is just great
if you have a bunch of junk on your screen. One thing you'll notice though is we're still in
appdb. And that's because MongoDB is kind of interesting compared to other things like SQL,
and that's that you don't actually need to ever create a table. You don't need to create
databases. You don't need to create collections. That's because whenever you try to access
something, if it doesn't exist yet and you try to put data in it, it'll just automatically create
itself. So if we try to add data to this appdb that we're currently inside of, it'll now recreate
that database. Because as you can see, if we do show dbs, we have no databases. Now we can actually
try to add something to this database and it'll recreate that database. Now the last command I
want to show you before we get into creating data is just the exit command. You type that in and it
exits you out of the terminal. And we can come back in here with mongo.sh and re-enter that
terminal. So that's just one really simple command that you can use to exit out of the terminal.
So now that we're in the terminal, I just want to type in use appdb and it's going to use that
app database. You can call this whatever you want. It really does not matter. And that's just because
if we type in show dbs again, you'll see this database doesn't exist yet. We're just going to
be using it and then adding data to it, which will automatically create that database for us.
Now to access the current database you're inside of, you just type db. That gives you the current
database. And this has a bunch of functions essentially on it. So we can say db. and we
can say the name of a collection. So a collection, like I said, is a table. So we can call this
whatever we want. We're going to call ours users, where we're going to store all of our user data.
And then to insert into this table, you just type in .insert1. And this allows us to insert
one single record inside of this users collection. And this insert1 is a function. And inside this
function, all you do is you just want to pass it an object. And this object is just a JavaScript
object. Essentially, it's like JSON formatted. So what we want to do for our object, let's just
pass in a name here. And the name is going to be John. And we hit enter. And you can see it says
true and inserted that with this ID right here. So now if we wanted to be able to see this
information, we can just say db.users to access the collection. And we can call the find method.
The find method just gets every single thing inside of the database. And as you can see,
we have an array with one object, that object has a unique ID that's automatically generated.
And then it has the name of john. And this underscore ID is something that's automatically
generated by MongoDB every time you insert a record. So that's really nice. You don't have
to worry about generating unique IDs, it just does it for you. Also, something interesting
about MongoDB that you may have noticed is we didn't have to define any columns, we didn't
define any schema, we just said insert this JSON object that has a name, and the name is john,
there's no schemas, there's no columns that you have to worry about in MongoDB. And instead,
you worry about things called documents. Essentially, every single object you store
inside of a database in MongoDB is called a document and documents live in collections,
and collections live inside of databases. So we have our app DB, which is our database,
users as our collection, and this john document right here, that is our document inside of our
users collection. And because there is no schema, it means you can actually add anything you want
inside of your collection. And they don't all have to have the same columns or the same fields.
So we can come in here and we can say DB dot users, insert one, whoops, insert one,
and we want to insert a brand new user. And this user is going to have a name.
And we're going to give them a name of Sally. So so far, this is exactly the same as john,
it has just the name field. But we're going to add additional information. For example,
we're going to put an agent here, and we're going to say that they are 19 as well. And then we're
going to add in more information, we're going to add an address. And one really nice thing about
MongoDB is you can actually nest things inside of it. Because just like JSON, you can put objects
inside of objects. So we're going to nest an address object inside of our user right here.
So we're going to say the address has a street. And this street is just going to be 987 North
Street. And then we're going to close off that object. And we're also going to put an array of
hobbies inside of here. So let's put an array of hobbies. And we're just going to put one
hobby inside of here, which is running. And if we just close this off and run this,
you can see acknowledge true and inserted that user. And if we do db dot users dot find,
you can now see we have john up here is our first object. And then we have Sally down here,
which has the name Sally age 19, the address, which has the street inside of it, and then our
array of hobbies. So with MongoDB, we don't have to have all of the documents inside of our
collection share the same fields. And also we can nest things such as nesting an address inside of
user or nesting an array of hobbies inside of here, which is something that's pretty much
impossible or very difficult to do in SQL. Let me just clear my screen real quick and get back to
the top. And I want to talk about how you can insert multiple records at once. So if we go
db dot users, there's another function called insert many. And this is just like insert one,
but it takes an array of objects instead. So we could say that we're going to have a name here of
Jill. And then we're also going to just close that off and pass in another person which has a name
of Mike. So now essentially, we're passing an array to this insert many which has one object
here of Jill and one object of Mike, we hit enter, you can see it inserted both of those.
If we do db users dot find, you can see that we now have Jill and Mike at the bottom.
Now in order to populate our database with a little bit more information, I'm just going to
do another db dot users dot insert many. And I'm actually just going to copy over a bit of
information, just a few different objects. So that way, we don't have to manually type these in.
And essentially, I'm just inserting a user here. That is myself, Kyle. And then we have another
Billy down here. So if I hit enter, and just do db, or first all clear, and then do db dot users
dot find. And as you can see, if we type in an error, it's really nice, they have really good
error handling. So it tells you, hey, you just messed up your error right there.
So now you can see we inserted this user Kyle, which has age 26, hobbies of weightlifting and
bowling, and then we added an address. Same thing here, Billy has swimming, bowling age 41 and
another address. This is just so when we do more complex queries, we can actually query in all
these different pieces of information for our database. Now speaking of querying, that's the
very next thing I want to do, how do you read information from your database? Well, we figured
out that users has a find method. And this find method just gets every single thing from the
database as we can see. But what we can do is actually additional information on top of that.
So we can say db dot users dot find. And then we could call additional message after this,
for example, if we wanted to sort things, or we wanted to limit, we could say, you know what,
limit me to only two results. And now you can see we're getting the first two results in the
database. But what if we wanted to sort them by, for example, name, so we wanted to get them in
alphabetical order, what we could come in here, we could say db dot users dot find, and then we
can just say sort. And for sort, this takes an object. And this object has a key, which is the
thing you want to sort by. So for example, we want to sort by name, and then you pass it either one,
or you pass it negative one, depending on the order that you want to sort in. So let's just
pass in one at first. And then we're going to limit this as well to two. And as we hit enter,
you can see we get Billy first, and then Jill, and that's going in alphabetical order. Now if
we pass in negative one, it's going to do the reverse order. So now you can see we get Sally
first, and then Mike next. And that's because it's going in reverse alphabetical order. And a
really nice thing that we can do is we can actually sort by multiple fields at once. So if we just
come in here, we wanted to sort by name, and then we wanted to sort by age, for example. So let's do
age first, we're going to do that in order and then name second in reverse order. Now you can
see that we're getting Mike and john showing up here. And that's just because they don't have an
age. So they're showing up at the very beginning, we change our age here to negative one, you can
see that now it's sorting in age reverse order first, so it's going to 41. And then 26. And if
we had two people with the same age, then it would also sort them by name in reverse order as well.
And the last thing I want to talk about of these sorting type methods is skip and skip just allows
you to skip the first few things. So let's say db dot users dot find, and we're going to limit this
to just two, you can see we get john and then Sally. But if we put in a little skip here,
let's just say we want to skip one, that's going to skip the first entry. So now we start with
Now these messages are great. But really, what we want to do is build a query on different fields.
So we want to do essentially where queries if you're familiar with SQL. To do that, we just
pass a parameter to our find method. So we're gonna say db dot users dot find. And here we just
pass an object to find. And this can get really complex or be pretty simple. But the main thing
you have to worry about is you want to pass in the thing that you want to find by let's just do
name. And then you can pass it a value. So let's just say Kyle, what this query does is says,
hey, find everyone where the name is equal to Kyle. And as you can see, we get just one object
where the name is equal to Kyle. That makes sense. We can also query by additional things, you know,
we can come in here, we can say age, and we wanted the age to be for example, 26. And now you can see
we get Kyle again, because he has the age of 26. But it's important to note this age is a number.
So if I tried to query where the age is a string of 26, we get nothing back. And that's because
the age is a number again. So it's only going to match where the age is equal to this as a number,
not as a string. Now also, if you're familiar with SQL, you're probably familiar with the
select syntax where you can select just a single row or a single column from the database. So I
want to get just the name or just the age where the name and the age but not the hobbies. Well,
we can actually do that as well. We can say db dot users dot find. And we're just going to find
where the name here is equal to Kyle. And then what we need to do is pass a second parameter,
which again is an object to find and this object here tells us what parameters we want to get.
So let's say we want to get the name field, we're just going to pass a one to that we pass a one
here that's saying, hey, we want to get this field. So let's get the name and the age. And that's the
only things going to be returned. As you can see, we get name of Kyle age 26. But the hobbies and
the address are not returned. You'll also notice the ID is returned automatically. If you don't
want the ID be returned, just put underscore ID and then set this to zero. That's saying,
hey, don't get me the ID field. Now as you can see, we get just the name and age without the ID.
Also, if we wanted to come in here and we said, hey, you know, I want to get all the fields
except for the age, I don't care about the age, we just put age zero here and nothing else,
it's going to say, hey, get all the fields, but don't get age. So as you can see, we get name,
hobbies, address and ID, but the age field is left out. So you can either use ones to say you
want to select only these fields, or you can use a zero and say, hey, select all the fields except
for this. And this is a pretty straightforward where cause we're saying where name equals this,
but what if we wanted to do something a bit more complex? Well, that's where we can do complex
instead. So let's say db.users.find. And again, we're going to pass this an object. And inside
this object, we're going to say, hey, we want to query on the name field. But in order to do a
complex query, what we need to do is put again, another set of an object. And this object is going
to contain all the information for the complex query. And inside of MongoDB, pretty much every
single complex query is going to start with a dollar sign. And then you're going to denote what
you want to do. So if we want to check for equality, just like we were doing before,
you can say dollar sign EQ. And then we can pass in here that we want the value to be
Sally. And what this is saying is, hey, get me where the name is equal to Sally,
just like we were doing before. And as you can see, we get Sally returned. But we can also do
something else. For example, we could do not equal. And this is going to get us everything
that is not equal to Sally. So now if we hit enter, and I make sure to change this to ne
instead of neq, you're going to see we get all of the users returned except for the ones where the
name is equal to Sally. Now there's a bunch of these different types of complex queries. So I
want to cover as many of them as I can as quickly as possible. So we'll say db.users.find. And
we're going to find here on the age property, because that allows us to do a lot of these.
And again, we want to make sure that we have an object. And this object, we're going to do
greater than so dollar sign greater than, and we're going to say everything that has an age
greater than 13. And as you can see, we get only the users that have an age greater than 13. We
could also do greater than or equal to which is GTE. So if we did like 19, for example, you can
see greater than or equal to 19 returns 19. We can do less than or equal to as you can see only
Sally's return because she's the only one with an age less than or equal to 19. And if we do less
than we get nothing returned because nothing has an age less than 19. Now also some more queries
that we can do, we just come in here db.users.find. And let's say that we want to query on name this
time. And we want to say where the name is equal to one of two values, we can use dollar sign
in. And that essentially says, hey, if the name is in this list of names, then return it. So we can
pass it an array, we can say Kyle, and we can say Sally. And this just says, hey, is the name Kyle
or is it Sally? If so, return it. And as you can see, we get Kyle, and we get Sally being returned.
We can also do essentially the opposite of in by saying not in by just putting an N in front of
this now gets us all the users that don't have a name that's either Kyle or Sally. And as you can
see, Kyle and Sally are the only ones left out of this list. Another query that's really important
that you're probably going to want to do, we'll just say db.users.find. We want to find by age,
and we want to check to see, hey, which ones have an age that exists, because as you know,
some of our documents don't actually have an age. So to check for exists, we just do dollar sign
exists. And we say true. And this is only return only the objects that have an age on them. As you
can see, all three of these objects have an age, we pass false to exist. Now it's only going to
return just the objects that don't have an age. So we can check whether or not there is an age
or there isn't an age. And this is going to return only things that don't include the key.
One thing to note though, is this is only going to check to see if the key exists. If we have an
object, for example, a name here of John and the age is set to null, exist true is going to return
the age that has null because it only checks to see if the key actually exists on the user,
and not if the value exists. I can really easily show that by just saying db.users.insert1. And
we're going to insert something that has an age that is set to null and a name, which is just
going to be bill. And if we just close that off, and I do that check where exist is true,
you can see even though the age is null, it still returns when we check for exist true up here. Now
the next couple of queries I want to talk about are going to be pretty complex queries when you
want to combine together multiple queries. So say db.users.find, we're going to do a find here on
age. And again, we're going to wrap this inside of an object. In this object, we want to check
to see when the age is going to be less than a certain value and greater than a certain value.
So we can say gte. So we want to check is it greater than or equal to the value of 20. And we
want to say, hey, which check also less than or equal to have 40, we hit enter, you can see we
get all the users that have an age greater than 20, and less than or equal to 40. And the way
this works is if you pass an object to a field, essentially does all of these as an and query,
so it's going to say, okay, where the age is greater than or equal to 20, and it is less than
or equal to 40, do this query. And if we combine additional things on this, for example, we pass
a name here, we say the name must equal Sally. Now it's saying that the age has to be greater
than or equal to 20, less than or equal to 40. And the name has to be Sally, we change this to Kyle,
we'll see that we actually get one result because this name is equal to Kyle and the age falls
within this range specified. This is one way where you can do an and query. Another way that you can
do an and query, though, if we just clear our screen here, we say db.users.find. And inside
this find, we're going to pass the dollar sign and and when we use this dollar signed and
essentially, what we're saying is, hey, pretend that we're doing another find. And inside of here,
we're just combining all the things inside the end. So and takes an array. And in this array,
we can just do what we did before in a find. So we can say, you know, we'll do age, which is equal
to exactly 26. And we'll come in here and we're going to say, name, let's make sure this is an
object is going to be equal to Kyle, make sure I close this off. Now you can see we passed in
which takes an array. And this array has an age of 26 and a name Kyle being passed in. So it's
combining both of these together. And as you can see, we get Kyle being returned. Generally, you
don't really need to use this dollar sign and very much. And that's just because you already can do
and almost every other way by just putting them all inside of one query. But one thing that you
do need is or if you want to do an or you're going to need to use the dollar sign or syntax.
So let's say the age here is going to be less than or equal to 26. Or the name is going to be Kyle.
And actually, let's make this less than or equal to 20. There we go. So now we're saying, hey,
I want to see where the age is less than or equal to 20. Or the name is Kyle. And of course,
we're getting an error. That's just because I need to make sure here I close off this object.
Now you can see we're getting Sally returned because her age is less than 20. And we're
Kyle returned because the name is equal to Kyle. So the or syntax is really important,
because there's no other way to do or unless you use the dollar sign or here. Another thing you
can really easily do, but just say db dot users dot find is if you want to do a not query. So
let's say that we want to check here, when the age is going to be less than or equal to 20.
And let's say we actually want it to be not less than or equal to 20. Well, what you can do is you
can just create another object and put the not inside of it. And you just pass an object to not
essentially what not does is it just negates everything that's inside of it. So now we hit
Enter. And I make sure that I add in the correct number of curly brackets, you can see we're
getting all of the users where the age is not less than or equal to 20. So we're getting all
the users where the age is greater than 20. We're also getting all the users where there is no age
defined. As you can see here, these users have no age and this one has an age of null. While if we
for example, did something like greater than 20, which is essentially the same thing, but we remove
this not and hit Enter, I make sure that I have the object here. You can see now we're only getting
users that have an age defined. So that's one thing that not can kind of do differently. So if
you do a not, it'll actually return all the things that don't have ages in this case. Really, for the
most part, though, you don't ever really need to use not because less than greater than not equal
to equal, they already all have the not a version, but it's good to know that it exists. Now the last
complex one of these queries I need to talk about requires me to add a little bit additional data
to our database. We'll say db.users.insertMany. Inside of here, I'm just going to be inserting
users that have a balance object as well as a debt object. So if we hit Enter, you can see we inserted
these two users. What I want to do is I want to find the users where their debt is greater than
their balance. Essentially, they're in debt. So to do that, we can say db.users.find and in order
to compare two different properties on an object, what we need to do is we need to use the dollar
sign expr. That stands for expression. And inside of here, you can run a query that compares two
different things. So let's say that we want to do a greater than query. We want to see where the
debt is greater than the balance. So we can say greater than. And inside of here, we're going to
pass it an array. And this is going to say, hey, where the debt column is greater than the balance
column. So the first thing we pass to this array is going to be the first column we want to check
to see if it's greater than the second column. We hit Enter. And I make sure that I close everything
off. You'll see now we're actually getting all of our results being returned. And you're probably
wondering, why is this? The reason is, is because if you want to actually access a column instead of
a specific value, you need to put a dollar sign in front. So now you can see we have dollar sign debt
and dollar sign balance. Now when I hit Enter, I only get the object being returned that has a debt
of 200 and a balance of 100. So the debt is greater than the balance. So if you're going to be using
columns, just make sure you use that dollar sign syntax in order to do that. Now, if we just clear
this out, I want to talk about a few additional find methods that are just going to be continuations
on what we've already talked about. So we can say db.users.find. And if we want to find based on
a nested field, so example, we have address.street that we want to query on. Well, in order to make
this query, we just wrap this address.street name in quotes. And we have an address object,
and inside it has a street property. So you can say address.street, just like you're going to be
accessing this with JavaScript. And then we can use all the additional queries we talked about.
So we get set to a specific value, for example, main street like this. I close that off, you can
see we get the user that has 123 main street as their street. Or you know, we could combine this
with you know, less than equal to is greater than we can do all that additional stuff passing it in
just like this. This address.street is just like any other column name. Also, something that we
can do is we can do a find one so that this allows us to just find one object that matches the query.
So let's just do here a find one, and we're going to pass it in age. And we're going to say where
the age here is less than or equal to 40. For example, close this off. Now you can see it just
found the first user that has an age less than or equal to 40. So if you did some kind of sorting,
for example, you could find the first user that has an age less than or equal to 40. That's what
find one is going to do. And then also we have the ability to count. So instead of find one,
we could change this to count documents. Now we hit enter. And you can see there are two people
in our database that have an age less than or equal to 40. Now that right there pretty much
covers everything you need to know about querying data. So next we want to talk about updating data,
which is of course an incredibly important part of any database. So in order to update a field,
we just want to say db.users, and we have a method called update one. This allows us to update to
just the very first thing. Just like find one, this is going to be the same thing but for updating
one object. And the first parameter to update one is just going to be the thing we want to filter
on. This is the exact same thing that you pass to find. All the things we talked about inside a dot
find work for this update one. Let's say we want to get the user where the age is 26. This is the
user with the name of Kyle. So where the name is 26, we want to update them. And then we can pass
in here all the update things we want to do. And this is kind of confusing for most people because
you would think, okay, we'll just say, you know, age is going to be 27. And that's going to work,
right? But this is actually going to throw an error. As you can see, update document requires
atomic operators. And that's because they require dollar sign operators. So in order to do a set
operation where you want to set a field, for example, we want to update our age to 27,
you need to use dollar sign set and then pass it the things you want to update. So in our case,
we want to update our age to 27. Hit enter, you can see it updated. And then if we do a db.users.find
one, and we're just going to do where the age is now 27. You can see we got that user Kyle,
which has an age of 27. And up here, if usually if you're going to be doing an update, you're going
to want to update based on the ID. So we could say where the ID is equal to and we'll just copy
this ID right here and paste that in. And now we can set for example, the name to new name,
hit enter. And we're going to do a find based on that ID as well. So we'll say underscore ID
equal to the ID we have here. Now you can see the name has been updated to new name. Now there's a
few other things you can do for updating. So we'll say db.users.update one, we're going to pass in
that underscore ID again, because we want to get just that one user. And instead of doing a set
here, I'm going to do an increment. So I can actually increment the value of a number. So I
want to increment our age property by let's say three. So now we're going to go from 27 to 30.
And if I just close this off, hit enter. And I find that user. So we can say db.users.find one,
you can see now their age is 30 is incremented it by three. Another really cool thing that we can do
with update one instead of increment is we can rename a column. So I want to rename, let's say
the name column, I want to rename it to first name, just like that. If I make sure here that
I spell rename properly, hit enter. And now I find that user, you can see that their name property
has been renamed to first name. Another thing we can do is unset a property. So if we just say,
we're going to do db.users.update one. And instead of rename here, we're going to do
unset. And I want to unset the property here, which is going to be age. So we're just going
to say age. In order to make sure we do this properly, we actually need to pass this in as
an object and just set the value to anything, it really doesn't matter, we'll just do an empty
string. So now we're unsetting the age property, we're just removing it completely. So if I just
make sure I have enough closing parentheses here, and I find that user, you can now see we completely
removed the age property, it didn't set it to null, it completely removed it from the object.
Another thing that we can do is actually add two arrays. So if we just come back to this
update here, we can say that we want to push a value into our array. And inside of here,
we just want to say our array is going to be called hobbies, because that's the array right
here, we want to push to this hobbies array. And we want to push in the value of let's say
swimming, for example, we just hit all those parentheses, make sure they're closed off,
hit enter. Now if we find that user, you can see we added swimming to the end of the array,
we can also do essentially the opposite of removing. So if we just get that update again,
instead of push, we can say pull. And let's say that we wanted to pull from here something like
bowling. So we'll just type in bowling, hit enter. And now if we find that user, you can see
we removed bowling from the list. And the interesting thing about this push and this
pull is that it takes the same queries as find one. So if we had like an array of numbers,
we could say, hey, remove everything less than or equal to the value of three, and it's going
to remove all of those. So again, all of those queries that we talked about in find, they work
all over the place in MongoDB. Now the next thing I want to talk about is updating many users at
once. So we can just say update many, and this is going to update everything that matches a
specific query. So let's say we want to get them where the address exists. So we're just going to
say dollar sign exists, we're going to set that to true. So every single thing that has an address
is going to be matched by this query. And all we want to do is just unset the address. So we're
just going to remove the address from everything. We're just going to say address, and just put an
empty quote inside of here, close this off, hit enter, you can see we've updated three different
things. And now if we do a db.users.find to get everything, you'll notice none of our users have
an address, because we got all the users that had an address, and we removed that address field. So
that's what update many allows us to do. And then finally, we have replace, which is kind of
interesting. So let's just say db.users.replace one, and this is how we replace one single object.
Let me just actually clear this out. So we'll say db.users.replace one, and replace one takes the
same exact filter. So we can say age is equal to 30. That's the user has the name Kyle. And then here
we just pass it a new object. So let's just say the name here is going to be equal to John. And now
if I hit enter on this, if I just close this off, hit enter, you can see it worked. And now I can say
db.users.find, we can say name is equal to John, close this off. And you'll see we get this user
here. What happened is we found the user with the age of 30, and then replaced that entire object
with whatever we passed in here. So since we only passed a name of John, that's the only thing that
was saved to that user. So replace essentially takes everything at that location, deletes it,
and then replaces it with what we pass into replace. Generally, 99.9% of the time you're
going to want to use update instead of replace, because you don't want to actually replace an
entire object, you just want to update a few fields at a time. Now that's everything that we
need to know about updating. So let's move on to deleting. So we can just say db.users, and we can
do delete one to delete one user, and it takes the same exact thing as find. So we could say here,
name is equal to John to delete that user we just added. You can see it deleted one user,
db.users.find, where the name is equal to John. And now you're going to see nothing gets returned
because we just deleted that user. We can also do a delete many, so db.users.deleteMany. And here
we're going to delete everything that has no age. So we're going to say age, we're going to check for
exists, so we'll say $exist, false. So there's going to be anything that doesn't have an age
is going to be deleted. You see it says it deleted five different things. And if we say users.find,
you can see now we only have users that have an age. Even if the age is null, it just has an age
key on it. And that right there is everything you need to know about MongoDB. If you enjoyed this
video, I highly recommend you check out the full cheat sheet on MongoDB I have linked in the
description below. And with that said, thank you very much for watching and have a good day.
