- [Instructor] Okay. The last end point
we're going to convert over to use MongoDB
is going to be the end point for removing items
from a user's cart.
Remember, that's the app.delete end point that we had here.
And again, this one is going to be pretty similar
to the other end points from the point of view
of connecting to the database.
The main difference again is going to be the type of queries
that we make to the Mongo database.
So here's what we're going to do,
we're going to start off again by adding
the async keyword to the callback,
and then we're going to delete this existing logic
for how we got the cart items,
and then we're going to go back up and copy
and paste the logic for connecting
like that,
and at the end we're going to put client.close.
So the only part that we have to define now
is the actual logic for making queries to MongoDB.
And what this is going to look like ,
is something like this.
The first thing we'll want to do
is actually get the user ID out of the URL parameters,
which we weren't doing before.
And then down here, we're going to say,
await
db.collection,
and we're going to get the users collection here.
And we're going to say update one,
and we're going to update a user here whose user ID
matches the user ID in the query parameter string.
Okay. And the way that we're going to update the user,
this is somewhat similar to what we did up here
with add to set,
except instead of adding an item to the users array,
right? We're going to pull an item from that array.
So we use the dollar sign pull thing
that MongoDB provides us with.
So what we can do is that's going to be the key,
we're going to say pull, cart items,
product ID.
So basically what this is saying here,
and don't worry too much about the syntax
if you're not familiar with it.
It's saying that we want to pull the product ID
from the cart items array on the user that we selected here.
Okay?
So essentially what we're doing
is we're deleting the product ID
from the user's cart items array.
And now that we've done that,
we want to send back an updated array of the user's products
to the client.
So what we're going to do is pretty similar
to what we've done in other end points.
We're going to say
const user equals await db dot collection
users
dot find one,
and we're going to find the user
with the user ID
in the URL parameter.
Okay. And once we found that user,
we're going to get their cart item IDs,
but first we're going to get the products.
Okay? So we're going to load all of our products from MongoDB.
So await db collection
products
dot find all of them
dot to array.
And then underneath that,
we're going to say const cart item IDs,
and get that from the user.
So user cart items,
and just like we did before,
we're going to convert those over to actual products.
And if you don't want to type it out again,
feel free to just copy and paste it like we did before.
Okay. So I'm going to copy and paste these lines here.
And actually that should be indented.
Let's copy that and paste it there.
And now we're just going to send back the cart items
to the client,
which is an updated array of the cart items
after we removed the one that was up here
in the URL parameter.
Okay. And that should be all we need to do.
So let's make sure our servers running,
npm run dev,
and we want to to make sure we save this file as well.
And let's go over to postman,
switch this to delete,
and we're going to send a delete request
to API slash users slash one, two, three, four, five
slash cart slash and then whatever the ID
is that we want to remove.
Let's remove one, two, three, for example,
then let's send that and it'll send us back an updated array
of the user's items without the one, two, three item
that we just removed.
Okay. And that's pretty much it for converting
all of our end points over to use Mongo DB.
Hopefully this has given you a pretty good sense
of what it's like to use a database driver
to store data on the backend.
Now, one question that a lot of people generally asked me
about using databases with no JS
is whether it's possible to use SQL
or some other kind of database with no JS
instead of MongoDB.
And the answer to this is a definite yes.
And while obviously using a different database driver
with no JS will look a little bit different
in terms of the connecting process
and the queries you write,
the basic formula of connecting to the database
via the driver and making queries to retrieve, modify,
or delete data will remain pretty much the same.
Anyway, that's just something to keep in mind
if you're ever interested in using a database
other than MongoDB with a full stack application.