All language subtitles for 027 Working with MongoDB_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,100 --> 00:00:03,810 Now, as a database, 2 00:00:03,810 --> 00:00:07,350 I will use MongoDB here and to be precise, 3 00:00:07,350 --> 00:00:10,980 I'll use their cloud offering, MongoDB Atlas, 4 00:00:10,980 --> 00:00:15,050 which is a fully managed cloud-based MongoDB database 5 00:00:15,050 --> 00:00:18,600 with which we can get started for free. 6 00:00:18,600 --> 00:00:21,260 All you need to do is click Try Free. 7 00:00:21,260 --> 00:00:24,900 This will then get you to MongoDB Atlas in the end 8 00:00:24,900 --> 00:00:27,900 and create an account for free here. 9 00:00:27,900 --> 00:00:30,170 So just enter some data about yourself 10 00:00:30,170 --> 00:00:32,063 and then get started for free. 11 00:00:32,900 --> 00:00:34,348 If you do all of that, 12 00:00:34,348 --> 00:00:37,100 after logging in, you'll end up on a page 13 00:00:37,100 --> 00:00:39,270 where you can create a new cluster. 14 00:00:39,270 --> 00:00:41,540 You must also automatically end up 15 00:00:41,540 --> 00:00:44,280 in that create a cluster wizard. 16 00:00:44,280 --> 00:00:47,680 And here you can generally leave all the settings 17 00:00:47,680 --> 00:00:48,550 as they are. 18 00:00:48,550 --> 00:00:52,100 Just make sure that you always pick the free tier settings 19 00:00:52,100 --> 00:00:54,140 unless you wanna pay. 20 00:00:54,140 --> 00:00:56,520 So for example, you can go with AWS 21 00:00:56,520 --> 00:00:58,370 and the default region here. 22 00:00:58,370 --> 00:01:01,850 And leave all the other default settings as well. 23 00:01:01,850 --> 00:01:06,054 Especially make sure that you're using the M0 Sandbox here. 24 00:01:06,054 --> 00:01:08,230 I can't select this here 25 00:01:08,230 --> 00:01:11,380 because I already do have a cluster created based 26 00:01:11,380 --> 00:01:14,770 on that sandbox but if you don't have a cluster yet, 27 00:01:14,770 --> 00:01:18,400 you should be able to select M0 Sandbox. 28 00:01:18,400 --> 00:01:21,590 And that is the cluster tier you should use then. 29 00:01:21,590 --> 00:01:24,040 And then just leave all the other default settings 30 00:01:24,040 --> 00:01:25,700 and create your cluster. 31 00:01:25,700 --> 00:01:30,000 Again, I already have one based on that M0 Sandbox here 32 00:01:30,000 --> 00:01:33,540 and therefore, you eventually should also have one. 33 00:01:33,540 --> 00:01:37,160 Now, that's the cluster, the MongoDB database cluster, 34 00:01:37,160 --> 00:01:39,120 which we'll use to connect to 35 00:01:39,120 --> 00:01:41,340 to then create our database in there 36 00:01:41,340 --> 00:01:43,173 and to store our data in there. 37 00:01:44,120 --> 00:01:46,850 Now, in order to be able to connect to this cluster, 38 00:01:46,850 --> 00:01:49,160 there are two things you also have to do here 39 00:01:49,160 --> 00:01:51,680 in this MongoDB Atlas console. 40 00:01:51,680 --> 00:01:53,370 Under Network Access, 41 00:01:53,370 --> 00:01:56,160 you have to add your local IP here 42 00:01:56,160 --> 00:01:58,010 by clicking on Add IP Address 43 00:01:58,010 --> 00:02:00,380 and then selecting current IP 44 00:02:00,380 --> 00:02:03,400 so that your local computer is able 45 00:02:03,400 --> 00:02:06,690 to send requests to MongoDB 46 00:02:06,690 --> 00:02:08,960 and under Database Access, 47 00:02:08,960 --> 00:02:11,800 you need to create at least one user 48 00:02:11,800 --> 00:02:14,180 with any username of your choice 49 00:02:14,180 --> 00:02:17,583 who has to have read and write access to the database, 50 00:02:17,583 --> 00:02:19,542 like this user here does 51 00:02:19,542 --> 00:02:23,180 so that this is a user with which we can connect 52 00:02:23,180 --> 00:02:24,633 to this cluster later. 53 00:02:26,270 --> 00:02:28,200 And once you did all of that, 54 00:02:28,200 --> 00:02:30,530 on Clusters, you can click on Connect 55 00:02:30,530 --> 00:02:32,470 and then connect your application 56 00:02:32,470 --> 00:02:35,543 to learn how you can connect to this cluster. 57 00:02:37,130 --> 00:02:38,730 And here we now actually need 58 00:02:38,730 --> 00:02:42,110 to use the Node.js MongoDB driver 59 00:02:42,110 --> 00:02:45,770 to write code here in our APIi route 60 00:02:45,770 --> 00:02:49,883 that does send queries to this MongoDB Atlas cluster. 61 00:02:50,910 --> 00:02:53,420 For this, we need to quit the development server 62 00:02:53,420 --> 00:02:57,160 and install an extra package with npm install. 63 00:02:57,160 --> 00:03:00,033 The mongodb package. 64 00:03:00,870 --> 00:03:02,930 This is the official MongoDB driver, 65 00:03:02,930 --> 00:03:06,420 which makes sending queries to MongoDB easy. 66 00:03:06,420 --> 00:03:07,370 Once you install it, 67 00:03:07,370 --> 00:03:10,520 you can start the development server again. 68 00:03:10,520 --> 00:03:13,510 And now this MongoDB driver allows us 69 00:03:13,510 --> 00:03:15,730 to connect to this cluster 70 00:03:15,730 --> 00:03:19,743 and then insert data or fetch data from there. 71 00:03:20,660 --> 00:03:22,860 For this in this api route file, 72 00:03:22,860 --> 00:03:27,860 we can import something from mongodb 73 00:03:28,770 --> 00:03:33,427 and that something is the MongoClient object. 74 00:03:36,780 --> 00:03:39,840 This is our object which allows us to connect. 75 00:03:39,840 --> 00:03:41,300 We can execute this code here. 76 00:03:41,300 --> 00:03:44,713 We can use MongoClient and call the connect method here. 77 00:03:45,920 --> 00:03:48,770 Now, the connect method wants this connection string, 78 00:03:48,770 --> 00:03:50,090 which we have here. 79 00:03:50,090 --> 00:03:52,360 So you should copy that string 80 00:03:52,360 --> 00:03:55,820 and then pass this as a parameter here 81 00:03:55,820 --> 00:03:57,033 to the connect method. 82 00:03:58,140 --> 00:04:00,570 Now, there, you need to plug in the username 83 00:04:00,570 --> 00:04:03,170 and password of your user. 84 00:04:03,170 --> 00:04:06,250 So that user who has read and write access, 85 00:04:06,250 --> 00:04:07,343 which you created. 86 00:04:10,100 --> 00:04:12,220 And I'll quickly do this 87 00:04:12,220 --> 00:04:16,269 without the angle brackets like this. 88 00:04:16,269 --> 00:04:18,480 Now, I will have changed my password 89 00:04:18,480 --> 00:04:19,500 when you're viewing this 90 00:04:19,500 --> 00:04:22,773 so you don't need to try to connect to my cluster. 91 00:04:24,120 --> 00:04:27,350 You also should replace myFirstDatabase 92 00:04:27,350 --> 00:04:31,500 with any database name of your choice, for example, meetups, 93 00:04:31,500 --> 00:04:32,713 but that is up to you. 94 00:04:34,210 --> 00:04:38,100 Now, when you do that, connect will establish a connection. 95 00:04:38,100 --> 00:04:42,440 And this is code which you never, never want 96 00:04:42,440 --> 00:04:44,430 to run on the client side 97 00:04:44,430 --> 00:04:48,100 because you would expose your credentials to your visitors, 98 00:04:48,100 --> 00:04:50,800 which is a security problem. 99 00:04:50,800 --> 00:04:53,450 But here in this API route, it's no problem 100 00:04:53,450 --> 00:04:55,480 because as I mentioned before, 101 00:04:55,480 --> 00:04:57,450 the code defined in here 102 00:04:57,450 --> 00:05:00,400 will never end up on the client side 103 00:05:00,400 --> 00:05:04,293 so this is a secure place to store credentials. 104 00:05:05,760 --> 00:05:08,500 Now, connect does return a promise 105 00:05:08,500 --> 00:05:11,300 and hence, we can turn our handler function 106 00:05:11,300 --> 00:05:14,610 into a async function to use await. 107 00:05:14,610 --> 00:05:18,620 This is possible and supported by NextJS. 108 00:05:18,620 --> 00:05:21,963 And then this will give us a connected client eventually. 109 00:05:23,560 --> 00:05:27,010 Now, on that client object, we can all the db method 110 00:05:27,010 --> 00:05:31,360 to get hold of that database to which we're connecting here. 111 00:05:31,360 --> 00:05:34,200 By the way, if that database doesn't exist yet, 112 00:05:34,200 --> 00:05:36,173 it will be created on the fly. 113 00:05:37,100 --> 00:05:38,780 And then on this database, 114 00:05:38,780 --> 00:05:41,563 we can get access to our meetupsCollection. 115 00:05:43,100 --> 00:05:46,760 Now, MongoDB is a NoSQL database 116 00:05:46,760 --> 00:05:51,000 that works with collections full of documents. 117 00:05:51,000 --> 00:05:54,910 Collections would be kind of your tables in a SQL database 118 00:05:54,910 --> 00:05:59,430 and documents would be your entries in those tables. 119 00:05:59,430 --> 00:06:00,760 So you have collections, 120 00:06:00,760 --> 00:06:03,010 which hold multiple documents. 121 00:06:03,010 --> 00:06:06,380 And a single meetup would be a single document, 122 00:06:06,380 --> 00:06:10,300 the overall collection then holds multiple meetups, 123 00:06:10,300 --> 00:06:11,853 multiple meetup documents. 124 00:06:12,720 --> 00:06:15,590 And you get hold of a collection by using your database 125 00:06:15,590 --> 00:06:17,920 and then the collection method. 126 00:06:17,920 --> 00:06:20,960 And the collection can have any name of your choice. 127 00:06:20,960 --> 00:06:24,000 Just as the database if it doesn't exist yet, 128 00:06:24,000 --> 00:06:26,580 it will be generated on the fly. 129 00:06:26,580 --> 00:06:28,563 And it could be named meetups as well. 130 00:06:29,460 --> 00:06:32,180 So it can have the same name as the database 131 00:06:32,180 --> 00:06:34,563 but you can also use different names here. 132 00:06:36,360 --> 00:06:39,274 And now that we got hold of the meetupsCollection, 133 00:06:39,274 --> 00:06:43,060 on that collection here, we can call insertOne, 134 00:06:43,060 --> 00:06:45,300 which is one of the built-in query commands 135 00:06:45,300 --> 00:06:49,830 for inserting one new document into this collection. 136 00:06:49,830 --> 00:06:51,990 And now the great thing about MongoDB 137 00:06:51,990 --> 00:06:55,220 is that a document is just a object in the end, 138 00:06:55,220 --> 00:06:57,520 a JavaScript object. 139 00:06:57,520 --> 00:07:00,400 And that now could be an object with title, 140 00:07:00,400 --> 00:07:02,323 image, address and description. 141 00:07:03,200 --> 00:07:04,560 And since that's the case, 142 00:07:04,560 --> 00:07:06,357 since that would make a lot of sense, 143 00:07:06,357 --> 00:07:10,040 we can also just directly insert data, 144 00:07:10,040 --> 00:07:13,980 so this full data object into our database. 145 00:07:13,980 --> 00:07:14,860 And hence, of course, 146 00:07:14,860 --> 00:07:17,793 we don't even need to use destructuring here. 147 00:07:19,530 --> 00:07:23,373 Now we would insert this data object into the database. 148 00:07:24,380 --> 00:07:26,820 Now, this also is an async operation. 149 00:07:26,820 --> 00:07:28,750 insertOne returns a promise 150 00:07:28,750 --> 00:07:31,350 and hence, we can await this here as well 151 00:07:31,350 --> 00:07:33,980 to get back the result of this operation. 152 00:07:33,980 --> 00:07:35,840 Result will be an object 153 00:07:35,840 --> 00:07:40,300 with, for example, the automatically generated ID. 154 00:07:40,300 --> 00:07:43,393 We can console.log result here if we want to. 155 00:07:45,160 --> 00:07:47,910 Now, we can also add error handling here 156 00:07:47,910 --> 00:07:50,340 to handle the case that connecting failed 157 00:07:50,340 --> 00:07:51,900 or inserting failed. 158 00:07:51,900 --> 00:07:54,050 And I do this in the full course 159 00:07:54,050 --> 00:07:56,700 but here, let's keep this focused and concise 160 00:07:56,700 --> 00:07:59,700 and let's simply assume that it will work. 161 00:07:59,700 --> 00:08:02,620 You can simply use try catch to wrap this 162 00:08:02,620 --> 00:08:05,200 to add error handling if you want to. 163 00:08:05,200 --> 00:08:07,780 Now, I wanna call client.close then 164 00:08:07,780 --> 00:08:10,650 to close the database connection once we're done 165 00:08:10,650 --> 00:08:13,610 and then we need to use this response object 166 00:08:13,610 --> 00:08:16,480 to send back a response 167 00:08:16,480 --> 00:08:18,030 because we're getting a request, 168 00:08:18,030 --> 00:08:20,370 we're then storing data in the database, 169 00:08:20,370 --> 00:08:24,140 ultimately, we also need to send back a response then. 170 00:08:24,140 --> 00:08:27,430 And we do this with this response object. 171 00:08:27,430 --> 00:08:30,770 Now, this works similar to what you might be used to 172 00:08:30,770 --> 00:08:33,020 from Node Express. 173 00:08:33,020 --> 00:08:34,610 You have a status method, 174 00:08:34,610 --> 00:08:36,419 which you can call on response 175 00:08:36,419 --> 00:08:38,799 to set a HTTP status code 176 00:08:38,799 --> 00:08:41,880 of the response which will be returned. 177 00:08:41,880 --> 00:08:44,730 For example, a 201 status code 178 00:08:44,730 --> 00:08:48,980 to indicate that something was inserted successfully. 179 00:08:48,980 --> 00:08:51,600 You can then chain a JSON call here 180 00:08:51,600 --> 00:08:54,910 to prepare the JSON data that will be added 181 00:08:54,910 --> 00:08:56,653 to the outgoing response. 182 00:08:57,510 --> 00:09:01,140 And here we could, for example, add a message key 183 00:09:01,140 --> 00:09:04,820 where we say Meetup inserted! 184 00:09:04,820 --> 00:09:06,700 But of course, it's totally up to you 185 00:09:06,700 --> 00:09:09,043 which kind of response you wanna return. 186 00:09:10,250 --> 00:09:13,860 With that, however, we have a basic API route, 187 00:09:13,860 --> 00:09:16,880 which will insert meetups into our database 188 00:09:16,880 --> 00:09:18,580 and therefore, in the next step, 189 00:09:18,580 --> 00:09:21,610 we can now send a request to this API route 190 00:09:21,610 --> 00:09:25,520 from the front end when this form here is submitted 191 00:09:25,520 --> 00:09:28,640 so that we actually do trigger this API route 192 00:09:28,640 --> 00:09:30,603 and we use that code here. 14914

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