All language subtitles for 027 Working with MongoDB_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now, as a database,

I will use MongoDB here and to be precise,

I'll use their cloud offering, MongoDB Atlas,

which is a fully managed cloud-based MongoDB database

with which we can get started for free.

All you need to do is click Try Free.

This will then get you to MongoDB Atlas in the end

and create an account for free here.

So just enter some data about yourself

and then get started for free.

If you do all of that,

after logging in, you'll end up on a page

where you can create a new cluster.

You must also automatically end up

in that create a cluster wizard.

And here you can generally leave all the settings

as they are.

Just make sure that you always pick the free tier settings

unless you wanna pay.

So for example, you can go with AWS

and the default region here.

And leave all the other default settings as well.

Especially make sure that you're using the M0 Sandbox here.

I can't select this here

because I already do have a cluster created based

on that sandbox but if you don't have a cluster yet,

you should be able to select M0 Sandbox.

And that is the cluster tier you should use then.

And then just leave all the other default settings

and create your cluster.

Again, I already have one based on that M0 Sandbox here

and therefore, you eventually should also have one.

Now, that's the cluster, the MongoDB database cluster,

which we'll use to connect to

to then create our database in there

and to store our data in there.

Now, in order to be able to connect to this cluster,

there are two things you also have to do here

in this MongoDB Atlas console.

Under Network Access,

you have to add your local IP here

by clicking on Add IP Address

and then selecting current IP

so that your local computer is able

to send requests to MongoDB

and under Database Access,

you need to create at least one user

with any username of your choice

who has to have read and write access to the database,

like this user here does

so that this is a user with which we can connect

to this cluster later.

And once you did all of that,

on Clusters, you can click on Connect

and then connect your application

to learn how you can connect to this cluster.

And here we now actually need

to use the Node.js MongoDB driver

to write code here in our APIi route

that does send queries to this MongoDB Atlas cluster.

For this, we need to quit the development server

and install an extra package with npm install.

The mongodb package.

This is the official MongoDB driver,

which makes sending queries to MongoDB easy.

Once you install it,

you can start the development server again.

And now this MongoDB driver allows us

to connect to this cluster

and then insert data or fetch data from there.

For this in this api route file,

we can import something from mongodb

and that something is the MongoClient object.

This is our object which allows us to connect.

We can execute this code here.

We can use MongoClient and call the connect method here.

Now, the connect method wants this connection string,

which we have here.

So you should copy that string

and then pass this as a parameter here

to the connect method.

Now, there, you need to plug in the username

and password of your user.

So that user who has read and write access,

which you created.

And I'll quickly do this

without the angle brackets like this.

Now, I will have changed my password

when you're viewing this

so you don't need to try to connect to my cluster.

You also should replace myFirstDatabase

with any database name of your choice, for example, meetups,

but that is up to you.

Now, when you do that, connect will establish a connection.

And this is code which you never, never want

to run on the client side

because you would expose your credentials to your visitors,

which is a security problem.

But here in this API route, it's no problem

because as I mentioned before,

the code defined in here

will never end up on the client side

so this is a secure place to store credentials.

Now, connect does return a promise

and hence, we can turn our handler function

into a async function to use await.

This is possible and supported by NextJS.

And then this will give us a connected client eventually.

Now, on that client object, we can all the db method

to get hold of that database to which we're connecting here.

By the way, if that database doesn't exist yet,

it will be created on the fly.

And then on this database,

we can get access to our meetupsCollection.

Now, MongoDB is a NoSQL database

that works with collections full of documents.

Collections would be kind of your tables in a SQL database

and documents would be your entries in those tables.

So you have collections,

which hold multiple documents.

And a single meetup would be a single document,

the overall collection then holds multiple meetups,

multiple meetup documents.

And you get hold of a collection by using your database

and then the collection method.

And the collection can have any name of your choice.

Just as the database if it doesn't exist yet,

it will be generated on the fly.

And it could be named meetups as well.

So it can have the same name as the database

but you can also use different names here.

And now that we got hold of the meetupsCollection,

on that collection here, we can call insertOne,

which is one of the built-in query commands

for inserting one new document into this collection.

And now the great thing about MongoDB

is that a document is just a object in the end,

a JavaScript object.

And that now could be an object with title,

image, address and description.

And since that's the case,

since that would make a lot of sense,

we can also just directly insert data,

so this full data object into our database.

And hence, of course,

we don't even need to use destructuring here.

Now we would insert this data object into the database.

Now, this also is an async operation.

insertOne returns a promise

and hence, we can await this here as well

to get back the result of this operation.

Result will be an object

with, for example, the automatically generated ID.

We can console.log result here if we want to.

Now, we can also add error handling here

to handle the case that connecting failed

or inserting failed.

And I do this in the full course

but here, let's keep this focused and concise

and let's simply assume that it will work.

You can simply use try catch to wrap this

to add error handling if you want to.

Now, I wanna call client.close then

to close the database connection once we're done

and then we need to use this response object

to send back a response

because we're getting a request,

we're then storing data in the database,

ultimately, we also need to send back a response then.

And we do this with this response object.

Now, this works similar to what you might be used to

from Node Express.

You have a status method,

which you can call on response

to set a HTTP status code

of the response which will be returned.

For example, a 201 status code

to indicate that something was inserted successfully.

You can then chain a JSON call here

to prepare the JSON data that will be added

to the outgoing response.

And here we could, for example, add a message key

where we say Meetup inserted!

But of course, it's totally up to you

which kind of response you wanna return.

With that, however, we have a basic API route,

which will insert meetups into our database

and therefore, in the next step,

we can now send a request to this API route

from the front end when this form here is submitted

so that we actually do trigger this API route

and we use that code here.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.