 All right. Hello. This is Christian. So in this video, we're going to connect to a Mongo database system using the MongoDB library for Node.js. Okay. So before we get, I want to go back to the website over here and just have a very quick review of the CRUD operations for MongoDB. So if you go to the site over here, there's just a link that will take you there. And you have, you know, all these functions to perform CRUD operations. Okay. So the insert, if you click over here, the left navigation menu, you have the insert, the select. This would be like a query for select, the update, delete, and so on. Okay. So again, MongoDB uses JavaScript functions to perform these type of operations. Okay. So just in here, if you do want to review, they're all here. Okay. So I'll refer to this one. We do some, any of these operations. But before we do that, though, I want to make sure that we have MongoDB running. So let's go here and look for Compass. If you have not installed it, make sure you do the MongoDB Compass is the preferred choice for interacting with MongoDB. So connect here. And in my previous exercises at demos, I have created a database called unit six DB under there. I have a collection or table called students. And then I inject and certainly about a hundred records or documents in here already for testing purposes. Okay. So you can, you know, create any database of your choice, any data you want to use, that doesn't really matter. But for this demo, I will be using this collection and documents to perform the operations. Right. So we'll keep this running. And now let's go to our IDE. So I'm going to go ahead and create a new folder here. We'll call this. I don't know. I'm just going to maybe call it db connect. Okay. So under here, I'm going to create the index.js folder for now. And then let's go ahead and initialize our, um, folder. So here, make sure you inside that folder, do the npm init dash y just to quickly instantiate that environment for us. Now we want to install a few things. Um, well, I'm not going to use the express, uh, framework because I'm just basically trying to connect to the database and then display some data for that purpose. Okay. So if you want to display this data to the DOM browser, then you want to go through the whole thing of creating a really nice site to do that for this example, which is going to connect to the database and retrieve data. Okay. So first let's install the mongo db. Okay. Um, that is all we need for this example. And so then now let's go ahead and instantiate our object over here. Okay. So let me turn this off for now. Let's do our usual setup. Actually, I need to install express. I'm trying. I meant to say express, um, I'm going to say express a handlebars is what I want to say, but I do need express. I apologize. Okay. So npm, I express for that. Okay. Close that here. Then we're going to have our app is instantiation of the app, create a port number. I'll use a 9,000 and I'll just put the URL. Um, let's see, uh, I'll put here the host. This would be local host, but I'll put the whole thing here. Okay. So we don't have to do that later. So local host, actually, um, it's better to use the IP address. Sometimes the local host does have some kind of conflict. So that is what we do. So plus the port number. Okay. And then we go ahead and app that listened to the port number, which is put here and then costs a little sample here console log host. I should say server, but that's fine. Okay. So that is the usual typical setup. And again, just want to just make sure you can see things in the browser. You can always log the, um, homepage. Um, so you can see it. I mean, for the browser. So, uh, just do something. Okay. So save and go to the terminal and let's connect and run this app. So again, if you don't have no month installed, uh, make sure you do that. It's really helpful. If you have it, just type no month, it should run your application. Okay. If you happen to run and you have this kind of example at this problem, then you can also try using MPX and then no month. And that also, uh, saw some problem as well. But anyway, so there this is running and load on port number, uh, 9,000. So if I go back to my browser and just go in local host, 9,000, you're going to see the message you put over here. Real tiny call. Hi. Okay. Do you see that that it's working? Then we are ready to go. Okay. So you can keep this running as I mentioned, and we can close that and we just keep updating outside. So first thing is you want to connect to MongoDB. So remember, we already installed the MongoDB driver. So we need to go ahead and import that up here. So I'm going to put at the very top here. Um, yeah, maybe up here. So it's not confusing with those other stuff. I'll put here the MongoDB client who will get the MongoDB client. A Mongo client is a object that is part of the MongoDB library. Okay. So if you do that, you're basically destructuring. We're just grabbing the object that we want. Okay. It's part of that. So make sure you put the curly brace around it. If you don't do this, then you can also create a variable called MongoDB client. But over here, you have to use a dot in the Mongo client. So either way is entirely up to you. This is just a variable assignment. I can assign to anything I want. A, B, C, doesn't matter. What I had earlier was you basically extract the Mongo client and put it right here. Okay. So that is the client. Now, I'll leave that up here. And then down here, let's put um, right down here because these are the APRs. So we're going to separate these. Okay. So right here is where you do the Mongo connection stuff. Okay. So first we need to get the URL to the MongoDB. So you put here const, um, MongoDB URL. I'll put that right there. So it's basically MongoDB colon instead of the HTTP. Um, and then we have the local host, which is same as above. I probably should put that into the variable. Maybe I don't want to retype this. It has the port number 27017. That is a default port. Okay. And then I also want to, um, connect to this MongoDB, uh, service, create a variable called, um, I guess quite a client. We're going to connect that to the Mongo client. So in the previous examples, or actually an older version, I guess, you can use the Mongo client to connect using callback functions. But I think the more modern ones are you going to call it asynchronously using async and await. Okay. So if that means we're going to go ahead and instantiate a new MongoDB object, uh, client, we'll pass into this constructor. Basically, it's a class of the URL. As you can see over here, the URL, which is the Mongo URL up there. And that's all we need. Basically, you can all ignore the other ones. So we are going to create an object. Once you get that, we need to connect to the actual database. Okay. So then we go ahead and do something like this. So client, actually, see, um, what do we do? Client. Connect. Okay. This connect function is part of the Mongo client. You can see it uses a promise, right? So what that means that you have to connect to the database asynchronously. Otherwise it will not work. Okay. So we need to do is the following. You can create a function, uh, async function like this. Uh, maybe call it like connect. Okay. And then inside this connect, if you're going to put that in here, and you're going to call these asynchronously using the await like that. Okay. And they're going to assign that result to a, um, another, I guess, variable called maybe DB. Okay. So we make the DB connection. And then once that condition is made, then we need to select a database. As you can see, we haven't selected one yet. We just made the connection. So once the connection has been made, then we need to go ahead and create a collection, I guess we call it call, call for collection. And when I again do the await, the only reason why we do, we're able to use this excuse is because this outer function must use the async in front of it. Okay. If you don't use that flag, it's not going to let you choose to await. Although I did hear that they were going to fix this update. So you don't have to make this a three async function. But as of today, you still need to do that here. Okay. So, um, what that does is that if you call this function connect, then, you know, you're able to connect to database asynchronously, you're going to wait until the connection is made before it goes on to the next one and so forth. Okay. So we go ahead and do DB.collection. We pass into this collection. Actually, no, I made a wrong. I had to be like, let's see, that's correct. DB is confusing. Actually, I should have, um, I should have, because this has to be like DB, that DB, I have to select DB first, I'm sorry. So the, the DB here is the local DB, the DB here is part of the decline. Okay. So a little bit confusing. I probably should use a different name, but I hope this makes sense. I'm going to pass into a database called my, actually call unit six, six DB, right? That's the one I called in my Mongo client over here. It's called Mongo, I'm a unit six DB, and the collection is called students. Okay. So that is the one I'm trying to reference here. So I'm going to connect it to that DB. So maybe they should have, they should have been said DB here. I probably should make it more clear. Um, let's see. Now that's confusing. Let's see. Let's put here connect. Let's put here connect. Sorry, all these free naming commissions. So I've made the connection, I select my DB, and then now I do the collection. Okay. Collection is going to be a wait. Um, uh, DB collection. And it's called, um, I call again, I think students. That is connection collection. And then now I need to make a, um, a, a query. Okay. So that, at this point, we have connected to the database. We have selected a collection. And now we need to make a query. So the query will be sent to a result. And again, we'll do a wait, a collection that find, find all of them, and we'll pass an empty object. You can leave like that. You can leave it out. It doesn't really matter. And I want to change or return that to an array. And then, and then we'll just get that. Then we can go ahead and console log or return this right to the browser. Okay. So this case is going to be a browser or actually no, because I did not send that to the browser. This would have been put inside here. Um, but maybe we'll just do a return. Okay. Return the result wherever it is being called. Okay. So also notice I did not trap any errors. If there's any error, you have to make sure that is taken care of. So to do that, we had to actually wrap the whole thing into a tri-cache. I should have done that again, try again, move all of these inside the tri. And then if there's any error, we're going to catch error here. And we're going to just console all the error. We're going to handle that error. And then we are good to go. Um, did I do it right? Okay. So we got that error taken care of. Okay. Let me format the code here. And then, so if that's the case, then we're going to return a, just return nothing at no value. It's a function, right? So when we get to that page, we can load that. So I'm going to go ahead and create another API to get our user. So put here, go to the students link. Once I get there, I'm going to let our const result is coming from the connect function. Okay. You call the connect function like that. So when it comes back, then we go ahead and then send it to the browser. It's over here. But again, notice this async function, we call it synchronously. So this function, this whole function here, it's a callback function. You want to make this into async as well. Right. So we can call this function asynchronously. I know it's really confusing, but that's how async work. Okay. So your async function can call other functions asynchronously using the await. And we'll see if this works. If there's any error, we'll catch it. Right. So kind of recap again, I'm creating a function to make a connection here. Okay. Can make a connection. Then we, actually, I might, might be wrong here, but we'll see. That's too many, too many, too many things. I don't think I have to do our way here. I think just kind of select the DB. I feel like that's not right. Okay. Collection. Maybe it's going to, I think you don't need to put await. I'm just basically selecting. I'm not doing anything here. This is a query. So you have to await for that. But the other two, I think just select. Let's see. Okay. So go ahead and then run this. Actually, it's still running. I'm going to go back to the browser and we're just going to test this link here. As you can see, this is that link. I'm going to go to the link that says students up here. Okay. So you can see the data came back already from the server. All right. So let me make it a little bit smaller. You can see it should have about 104 or three items in here all together coming back from the server. Okay. So it's working nicely. Again, you don't have to do that here. But I don't think it would hurt to do that. You can put all of these here to await. But it doesn't really matter. I think in this case, it should still work. Because I'm basically, you know, see, as you can see, it works just fine. Okay. So it's not necessary here. We can leave it. It's okay. It's just that we make this fine. You have to use the await. If I don't do that here, for the sake of I turn this off. All right. Let's see what happens. Usually that will hang your data. Actually, it comes. Actually, it's fine too. That's great. Okay. So it does come through. That's great. And maybe it comes to because, you know, I'm making a lot of async call here. But just to be safe, you could put a wait here. Okay. I mean, it won't hurt. So anyways, recap. We made the, we extract the Mongo client from the MongoDB library. Okay. We then create this URL to the MongoDB URL, the host and the port number. We create a new object of the Mongo client. We pass this connection string. This is a connection string to this client. So we can assign that to a client variable. Then we create a function called connect because you want to be able to connect these asynchronously. Okay. So if you don't put that in here, you cannot call this outside. Okay. In other words, you cannot do that here. Right. It won't work. So we created, we made the connection, assign that to the variable. And then from this connection, we select our database using that DB function. And then from this database, you need to select a collection called students. So again, collection is synonymous to a table. We assign that to a collection. And then from that table or collection, we can then perform the crowd operation. This is one of the crowd operation functions. I just showed you earlier. So we want you to find that result. We turn that into an array. Assign that to a variable. And then this function, we turn it to result. If it's successful, if there's any error here, then it's going to return nothing down here. You can put an empty object if you want. Maybe we'll do something like that. Okay. So let's just say I accidentally put a error here. Let's say that, I don't know, put T here for some reason. Okay. I mean, it may not be an error, but you're going to get a blank result. Maybe let's see. So do a refresh. You can see that I got a blank result coming back because there is no such thing as a students here. Okay. So let's do that. All right. So the find is one of the functions. If you want to filter, say I want to return only the students or a person that has ID of five, then you pass in here the option, the key values, right? So I want to get the, let's say, what are the options they have? We have, let's see, we have what? Oh, I made a mistake. Let's turn this off. This is just meaning has a, so everything's live because of the no monitor. Okay. So let's say I want to notice this underscore ID. This is the MongoDB primary key. And I didn't have one for my program because, you know, I did not choose to do that. If you go over here, you'll see that all the IDs are managed by MongoDB, but there is an ID field, which is, yeah, I managed, right? So it's called just ID without the underscore. We can look for ID of five, ID of 10, or we can look for all the genders or the IP addresses and so forth. Okay. So let's say we'll look for the ID only. So go over here and when I look inside the filter, where was it? Oh, right in here, find all the ID, put that underscore, okay, the ID equal to five, right? It matches that five ID. There is one. If there's not, you're going to get an empty result. So let's see if that is true. I'm going to refresh this page. Boom. And there it is, the ID of five for that particular student. I just did this performance operation by, you know, hard coding this in here. So this is like a get one type of IP, I mean, right? So this is like a get all students. This is like a get all. So therefore, when you, when you do the connection, when I call this connection string, I don't pass in any parameters. So this is, will be just something like that, right? Pass nothing to that. I get a get all. If you want to get one that usually you do something like this, if you remember, we're going to pass in here the ID of the particular student. And then maybe here, you want to connect, once you connect, you're going to pass in the ID of that particular, you know, student. So you can do like that ID is equal to recgrams.id, okay? And since the ID is a, if I'm not mistaken, is a number, measure is a number. So you have to convert that to a number, okay? So that means again, change that, pass that to a number. And then you can again, send that to the connection string up here. The same thing. You can use the same function, but when you do the find here, you have to pass in the option. So ideally, if you want to reuse this function over and over again for the find, then you put here, you probably want to rename this a bit more meaningful, but say that's what I put option here, okay? And the result is going to be an empty object and I can pass here the option here, okay? So if I do that, I didn't, you know, run this yet, but if I do that, I pass an empty object goes in here, I still going to get all the students. So it should still work just like before, okay? If I refresh this without the ID, you can see everything comes through, okay? So now this time, if I'm going to use this length to pass a single ID that I pass to this connection string, the object of ID is equal to ID, okay? So this object goes in here, right? Goes in here, replace this, and then it pass that to the find function. And now I'm going to return only the single ID of that student, okay? So let's go back to the browser. And now let's see, this is for all of them. This time I just want the ID for, so you can see it extract that ID only. If I want to get the ID of the 45th student, you get the 45 here, okay? If it's something beyond that, it doesn't exist, then you get an empty object, okay? So I just did the request for all and request for a particular student based on the ID field. And then if you want to select a different type, say you want to select by name, by first name or by a gender, for example, you put gender here, right? And then here, you will get the gender. And you know, it's a text, so we're not going to pass that to then we'll put your gender here. And you pass in the gender key, you put here the gender, okay? So again, I believe is case sensitive, if I'm not mistaken, we'll see. But let's say that I'm going to go over here and let's test first in the browser, all right? So that works fine. If I go and type in the gender would be for, actually, this would not work because if I type in like a mail, okay? It's going to be blank because it's thinking that it's going to the same IP, right? Same address. It looks the same. All it is just a different ID and gender here. So it would know which one to use. So if this is the case because the URL is, you know, the same, then it would never call this one, okay? So let's say that I want to call a student maybe by ID, you put ID ID, here you put gender or just G for gender or as a matter of gender or gender, okay? So a little bit more unique this time. So the different IP address. I mean, end point. So now let's go ahead and try ID of one. You're going to get that student, right? Now if you put gender of mail, you can see it returns all the mail, if I'm not mistaken, you can see over here, okay? And if you put lower case, you can see it's case sensitive, okay? So the female, again, all the female and so forth, right? So to avoid case sensitivity, then there is another thing that you need to do. Let's forget which one. You can convert that to a, you know, uppercase lower case before you send it over. Or I think there is a function called collation. And here you type in here the type would be, I forgot. I forgot what it is. It's a property called strength. You set it to one, okay? And yeah, there's another property. I forgot what it is. Let's see what that is. That will actually take care of that. Yeah, there's an error, as you can see. Okay, so that's good. Then go to the terminal. It will tell me. And the error down here, what does it say? Collation is not a function. Okay, so it's probably something else. Maybe it's for a different thing. Okay, well, I need to figure out what that is. I mean, you can use, you know, a regular expression to do that. But for now, it's just, just to make sure it matches the case sensitivity. There's a way to do that. Just forget what the actual syntax is. Okay, so I just show you here how you connect databases and how you can do all these operations work. Now to do the post and the put and the delete is the same thing, right? You already know how to do that for operations. Get the API or the setup. The only difference is that when you do that, the function here in here would be not fine, but, you know, update one, is the update, update one, update many, there is a delete one, delete one, delete many. Again, the option is the same as that one. Okay. And then there is also the insert one, insert many. So in this example here, I'm using find in here. So ideally the name of this would be find, not connect. If you just want to connect, then you would not go and proceed with these two statements, right? So when you do a connection, then that is supposed to be just connection. So if I connect, once I made the connection here, I can also select the database and the collection, and then we turn that back. In other words, do this. Okay. So let me update this. This is a little bit more specific. So if I just want to do a connection, then I'm going to return the collection back. Okay. So this collection here that we stop here. So that means when I do the connection down here, I wouldn't say result. It would just be a connection. I can call it, what should we call this one here DB, maybe if that makes sense. And we'll make the connection. No, I just call it, I just call it, I don't know. I guess DB is fine. Okay. So once we make the DB connection, then we got, actually we got the database and the collection back already. And then in this case, then we're not going to pass the option here. Okay. So to make this a little bit more usable, you will pass in here the database and the collection. You can set the default to these. Let's say this is my default database. If I don't include one, this is my default collection. If I don't include one. Okay. So this will be my variable called database. And then this is the collection for that particular collection. So make it more dynamic. When you make the connection, then I will pass in here. Right. If I don't pass in there is the default. If I do change it to a different database, you will pass both variables in here. Okay. So once I get the connection, then this statement here, okay, will go right in here. So now it's very specific to a get because I'm getting only one. So I'm not passing any options in here now. And then you should get the same result, except this time is not a deep collection, but it's called DB because I call it DB here. Okay. So that is for that one. And then same thing. This will be down here as well. So let me copy this and we'll be down here. And let me replace this. And then the option will be right here. And so we don't need this anymore. Right. We still need to make the connection. Okay. I'm making every connection here and for each call, usually you call it once and then that's it. Right. It should be connected. So I'm making like multiple connections, which is not ideal. Right. So what that means is that, by the way, let's let's test first. We'll do the same thing down here. Okay. So the first thing is connect the database. Let me move up here like this. And then I'm going to go ahead and copy this again right in here. And this I'm going to pass in this option to the find function. I'm going to destroy this. All right. So now a little bit better, right? Makes more sense. This is just the connection either. So here I would probably not return blank or return like maybe null. Okay. Return null return, you know, empty object will undefined or false. It doesn't matter. Right. So now if I go back and try the code, it should still work. Okay. As you can see, it's fine for mail. Okay. We'll still be fine if I put something else to get empty object. If I go back to the all the students coming back, fine. Get the ID of one. Okay. ID of 100, 100 and toward hand doesn't matter. So we'll still work. All right. So now again, you don't want to make so many connections here because the connection is open and never really close this. I made two connections. Every time I call this, I make a new connection. And that is not correct. I mean, eventually we'll close. But if you keep doing this, it's going to have a lot of connections. So when you make the connection here, and if it does succeed, you return, if it fails, then you log the error and then you return that. But before you return it, you want to close the connection. Okay. If there's an error, then you won't do something like this. If only when there's an error, because if there's error, it didn't connect. You want to go ahead and do like, what do we call it? This is the connection. So let's see. Client, con. If I made a connection. Well, if I didn't make a connection, I don't need to close it. Actually, yeah, you don't have to do that. If it does succeed, then you can close it later. But you can write different functions and do that. So what you can do is do something like this. So let's say that I want to put this outside in a global space. So that I'll call this here every time. It would do something like this. Let me create a function. Because I'm calling this asynchronously. So again, just like above, you have to do one or two things. I could use this and just re-devote this. Or create another one. And actually, we just do this, right? We just do a, let's see if I can do something like this. We do a db. Let db is equal to null. And then I'm going to invoke this function like this, wrap the whole thing, and then invoke it. This is called iffy. It's called iffy function, meaning immediately invoke function expression. I create the function. I wrap it with the parentheses, invoke it right away. This automatically invokes itself. Once I run that, I'm going to go to here and make the connection so forth here. So as I return the collection here, I'm going to set this global to that particular collection. So I'm not going to return it. I would do this. This is already the db. I don't want to call it db. Let's put here, I call it db, let's put it db connect. So this is actually, I can call it db. And if you turn that, you assign this global db with that db right here. Okay. So I invoke it right away. I made the connection, you know, select my database and then select the collection, assign that to a db variable up here in the global space. So when I do this part on here, I don't need to do that again. It's already connected. Okay. That's what I mean by creating one function and then that's the connection only once. So now if it's correct, it should still work just like before. As you can see, I go back and delete this. So you can see all the connection, all the students ID of 23 and so on. All right. So this is the better approach by just making one connection string call and then make that to a global. And then, you know, when you access these, you just basically go ahead and use it right away in here like this. Okay. So I think this is long enough. And if it's too long, feel free to, you know, rewind fast forward to wherever you need to do. So thanks for watching. Any questions, please let me know.