 All right, hello everyone, this is Christian again. So in this video, we're going to continue from the previous video where we connect database using the MongoDB library. This time, we're going to do something similar except we're going to use a program called Mongoose, which is an ODM or Object Data Modeling program. It's like a driver program, really, that talks between Node.js and MongoDB. It's a much more robust program and lets you do a lot more than just simple connections. So we're going to update this app here using Mongoose. So just to show you a very quick recap, last time we looked at this site over here where we did some operations, if I remember. So we're going to do something similar using the CRUD operations. So all these functions, the insert one, insert many, the update, delete, and so forth here. So Mongoose uses these similar functions to interact with the MongoDB database. So you see that using these Mongo functions are almost the same as the Mongoose. And that's a good thing, right? So, excuse me, first I'm going to go ahead and make a copy of this file. So we're just going to keep everything here almost of the setup. The only thing that we're going to change is the Mongoose, okay? So I'm going to make a copy of this and we'll keep the copy, but we're going to go ahead and change the original one, okay? So what I don't need here are the Mongo database. So we're going to scratch that. We'll keep the point number, everything here is the same. This is the same. So we're not going to use this, okay? I'm just going to show you the thing that I need. So these will be gone. Actually, let's just give it all of these here, okay? And then these down here will be a little bit different, but let's clean this up for now. And let's not confuse ourselves, right? So first of all, we need to go ahead and go to the terminal and install a library called Mongoose. So let's say this is to running. Let's see what is this one here? Okay, so app is still running, which is fine. I'll leave it running for now. And then we'll go to the, oh, I have so many now. Let's cancel all of them. Okay, so configuring. All right, so server side, we go to the DB Connect. Okay, it's going to do a MPM install and it's called Mongoose, okay? So typically you want to install MongoDB as well, but we already installed that already so we don't have to do that again, okay? So that's all we need. And we can confirm it in the package JSON file. We do have now MongoDB and Mongoose, all right? So, yeah, maybe I want to install course as well because we're going to try to access this API from the front end. So we also want to make sure that course is also installed, okay? To allow cross servers, all right? So, okay, so good, that's now good to go. And so let's update this a little bit. So we've got the connection going on here. I'm going to go and install import the Mongoose. I'll call the same name, require Mongoose. Okay, so I will do everything here the same file. Typically you want to spread this out into, you know, their own separate files, import them in. But I'll do here all in one file and then once you see the whole big picture, then if you want to refactor and move them around, we can do that, okay? So I'm going to move this down to the bottom. So we're not confusing with all these other APIs. Okay, so we got that going on. And then MongoDB, that looks good. All right, so let's do some of the middleware first, okay? So these are your middleware set up. So first we're going to use the express handle, express your encoder. This is for accepting data from APIs, from the external source, okay? You need to do that. The next one is to use the JSON file. The data should be in JSON format, the preferred data structure format. And then also want to get the, I'm not going to use it, but in case, just in case anyway, if you do want to use it, again, this is just very typical setup, okay? Public folder, I don't have one there as you can see, but you can put one there for any public data in the product interview. And then we'll also want to use the course, okay? So use and then see the course, you invoke that. So this is just basically two whitelist, all incoming IP addresses. That's all I'm doing here, okay? So that is all for that thing. And then I'm going to move this down here. Maybe it makes more sense to put it down here. So we're going to clean up a little bit here, and they want to do the DB connection here, okay? Using Mongoose, right? I'll put here Mongoose, so we know. Okay, so this is, okay, this is good. Now, typically when you connect to Mongoose, if you don't specify the database, then I believe by default, it will use the database called test, right? So typically you put right here at the end of the port number, like this, right? If you don't put it again, that's the default. If you have your own database, then you want to put it here, okay? So I'll call my unit six DB. That's the one I use. If you don't include it here, of course you can include it later on too, but I'll show you, if I remember, I'll show you. Okay, so we get that set up. And then the next thing I want to do is really just to make the connection, okay? So we use this object here to make the connection. Now Mongoose makes, if you call the connect function, I believe it will just make one single connection to your database. If you want to make multiple connections, like different connections to your database, then you want to put, there's a function called create connection, okay? So this one here allows you to create multiple different connections to your database. But for us, we're just going to create one. So we just call the connect right away. So it's going to run the connection. Inside this connect function, we're going to pass the URL, which is this connection string right here, right? So basically you put MongoDB URL and that is pretty much it. This is very simple like that. However, because you want to make sure that it's successful, you also want to make sure that you check and catch any error. So typically you can, because this function here actually returns what's called a promise, a promise object. And usually you need to resolve those promises. So basically you do a connection. And then over here, you put a download to that and then you put them, just put that one up and then you catch very similar structure. If you're using what the fetch API or promises, you have something very similar to this. So I'm saying is that you made the connection. If it's successful, then the result of this connection, which is the connection information about this database system comes in here. We put it into a result and then you can do something like this. You can show it or just say connected. We know. If you want to see what is inside and you would log the result variable and that would show a short. Maybe we just put here results, you can see it. And if there's errors, then we're going to catch it right down here. And then we'll just put here again. I'll have a function call and we handle error. Passing here the error. And there's a message object called message and you just want to grab the message and so you can see what that is. Okay. So let's put down here function handle error and takes the error. This is actually, yeah. It's the error already. We just put it just a message already. And we're just going to console log that. You do it, make it a little nicer, but I think this is enough for our example. Okay. So that is the handle error function. And then, yeah. So that's it for that one. So you made the connection. And the next thing I want to do is usually you want to create a speed schema. Okay. So I'll put a number here. So the number one, step one here. All right. One is you made the connection that you go together like this. And then step two, usually you're going to create a schema. Schema is like the structure or the shape of your data, right? So it's like a interface if you want to call it that. So the schema, we're going to give it a name. And in my example, we call it student, right? I think, or yeah, students. So I'm going to call a, what's called student schema. I'm going to call the mongoose up here. Has a property or a class or object called schema. So you're going to put, you're going to instantiate a new mongoose, that schema. Your pattern here is the object. Okay. And then step three, can type today, is you're going to create a model. The model is really just the actual documents. No, documents, what do you call this one? The collection, okay? Or we call this the table data, right? The actual data itself. This is the database. We need to create the model. The model is the actual table or collection. And then we can insert data to that, right? So the step four would be to, you know, up crud. After that, you can do crud, okay? So that is here, the schema. And so here is an object. So again, back to this again, what do we need? The object is the shape of our data. So if we go back to the mongoDB program, here is our data. This is the object, the shape of that. So I'm going to go ahead and basically, we'll actually, I'll show this. I can view this as in a, like that, okay? So I'm going to copy this, just copy one of those, and I'm going to paste it right in here. Where'd it go? Okay, right in here. Okay, so I probably should, I don't need the two curbs, it's just one. So this is our schema. This is the idea or the primary key that mongoDB handles. So we're going to ignore this one, okay? And we'll leave it out. So this is our information. So the key will stay as is, and then the values will here will be just the data type. What type is ID going to be? So in this example, this one is going to be a type number. So I could just, I could put a number like that, and that is good to go, okay? However, if you want to add more constraints to this ID, then you want to wrap this inside an object. And then this will have the property of type of number. And I also want to say this is also required. You put the required attribute that to true. If you also want to make it unique, then you put the unique here so that you don't have duplicates. Make that too true as well. Okay, so those are the things that I would add to this one. And then these will be just, you know, the strings. You put it again, type is going to be a type string and it's going to be required, okay? And you can set minimum maximum and so forth here. But I'll just, I'm going to just do that, copy, copy here. Email, maybe email should also be unique, right? You can have a duplicate email. I don't know, or you can make it unique. This is also string required. And then the IP address should require it, but then it may not be unique, okay? All right, so that is the schema. As you can see the data structure of this document and just the type. Or if you think about object, it's like the interface, okay? So once you get that schema, then we're going to build the model based on this schema. So over here, we can go to the next step is we're going to do a mongoose. Again, oops, model function. It takes a string of the name, which is the schema, which is called student. Actually, no, I made a wrong. The first one is the name of the table or the collection, okay? So we already have one called student. Now mongoose prefers you to use the lowercase name for your collection and also the singular form. I think that doesn't matter, but if you pull a singular form, the mongoose will automatically convert this to a plural form, okay? It's just how they work. So I call it student, call it student, student. If it's already plural, it's already plural. And if you already have a collection called students, then it's not going to override it, all right? It's not going to wipe out your collection or just use it. If it's not there, then it will create a new one, okay? So that is a security measure that you want to make sure that's taken care of. So I have, I call it students lowercase. So therefore I'm going to keep this as is, all right? So I'm not going to change it. It's not going to override either, but my schema is indeed the same structure as what I have already. If it doesn't match, then it's not going to destroy your current data. It's just that from here on, if you add more data using this schema, then the data will conform to this schema. That's what is kind of flexible in Mongo database, right? So that is for that one, and then that is it. And then usually you want to assign this to a variable. So you can use this again when you make all these operations. So when you do the crud, you need to know what table or what collection you are making the operations from is basically from this one right here. If you don't assign a variable name, you have to call this every time like this, right? Find, like that. And you don't want that. You want to say something like student, find. So you would put here const student is equal to that. So this is the student model, right? And then your crud will be find, update, delete, insert, right? Okay. So that is the typical setup for Mongoose. Now, I mentioned earlier that if you don't put your database here, then when you made the connection, Mongoose will automatically use a test database. And if you forget to do that, you may wonder, hey, what is my database? Where did it go? So that can be a confusing, right? So if you forgot to put here, then what you can do is you can put right in the connect function. The second parameter here is the object over here. And you can override the key called DBName. And you assign that with your database. So you need six DB like that or seven, right? If you don't have one, it will create one for you also. Okay. So you can do here or you can do here or you can, if you leave this out, then it will override whatever it was, which is the test one. Okay. So up to you. And we can leave it as is like this. You can see how this works. Okay. So now I think that looks good. So let's go ahead and test our program. I'm going to delete all this back again. So I'm not going to do this. Okay. So I'm just showing you that. That's how the functions for the crowd. So the crowd is basically these APIs down here. So we have this just very generic one. So here is to get all students, right? So we use, this is the DB, I call it DB before because I'm using the MongoDB library, but this sound called student. So here basically it's similar. We're going to be just student that find. And the functions are, I think it's identical. I think it's probably the same, but because Mongo's automatically converts these two object array already. So we don't have to say to array. If you do that, it doesn't work as far as I know. It just find and it'll be just like that. And you still have to call them asynchronously. Okay. That's why you will call by functions here must be async. So you can call them using the await. If you don't do that, it's not going to work, right? So that is that for the get same thing down here. You get the ideas as before and they want to change this to say student. And then again, we drop the array over here. Same thing here. You can see it's very similar to the MongoDB library. And that is it, right? Very simple like that. And we're going to test and see if this works. So let's save this and go to the terminal and let's run this. So in PX, node one, hopefully there's no error. Okay. Well, some errors already, core is not defined. Okay. Did I not import that in? Oh, I did not import yet. I use it right here, but I didn't import it. So good catch. Okay. That should solve it. And there we go. So you can see over here, it's already running and it ran, as you can see, it ran already. And then as you go down the code here, right? It goes down here here and then it goes and made the connection successfully connected and it sends the result to the console down here. What you see here, here's actually the result of this. So if I expand this all the way up and if you scroll all the way to the top you're going to see that when I made the connection actually it doesn't show all of them. But that is the result here. Okay, it's too long. So it's kind of like, I can't see all of them. Let me see. Again, see if it loads. There you go. See if I go up, how far it takes me up. Hopefully enough. And keep going until I find that message says connected. Is this some data already? Okay, data information about our database. Here's the unit 6DB. Number of stuff right here and then keep going. There it is. So I ran the nomon here and it ran the app and then connected this message here and then after that is this connection string information. Okay, so just to show us that it is working now. All right, so I'm going to turn this off. I don't want to see this again every time. So every time you say this program it runs it again and again. So it makes the connection every time. But once app is running, it only runs it once. It'll make the connection once until you reboot the server, right? So if you don't reboot it or if you don't close it explicitly the connection will stay connected. Okay, so now let's go and test these APIs down here. I think it's still launched before. So here is the API. And let's go back to the main students page. As you can see, it launches all the data here already about 103 of them. And if you want to get the student ID of one which is this guy over here who's a Jillian then you put ID, I think we have the ID and then slash one. Okay, then slash over 100, right? And then we have the gender, right? And again, it's case sensitive. And I figured out I had, I did it right yesterday but I just put it in the wrong place. So you can see all the mail. If you look at the gender, if I can find it where it is that it should, I show you all the mail over here, right? All these only mail. Okay, so here is where you had to make that case and sensitive thing. So when you use the find function, especially when you test for text strings, you can use the regular expression or you can set the collation over here. And you have passing at the object. And one of these is the locale. And we want the locale to be of type English because I'm using English text, okay? Otherwise you would choose like Russian or Arabic or Chinese or whatever you want to put it here. And then the second is the string property. And this could be the one or two, okay? It's just those two type. And what this does is that is that basically there's a lot more properties here. It's basically to let you type this data without case sensitivity, okay? So if I save that, go back to the browser and now if I search for mail, you can see everything comes to find. Again, just to make sure it does work, okay? So you can see now it's fine, right? So that is the crowd operations for the find and find all, find one, find all based on gender and so forth here. But typically, you can have all stats, IPIs, EPS, you know, let's say creative as you want. But we're gonna look at just the student here, the ID and wanna do some of the other operations like they put post and delete as well, okay? So for this example, I'm gonna ignore this one here. And I'm making simple, okay? So we're gonna pick based on the ID only. So we would do that. And then I'm going to push this all the way down. I don't need this here. Then we're gonna do the other ones. This is the post, go to the same IP address of the students. And we're gonna call this a think again. And then we're gonna have the rec fonts. Those things set up as the path. But this time we're going to add some data, right? And we're gonna do something like just so it does work. Okay, I'm gonna put here data. It's equal to one of those information. So the one I have up here, right? I can copy, actually let me copy one of the existing ones over here. I'm gonna copy this again. And let's do one more time. Just make sure I really have a copy of that. I'll put a writing here, okay? So again, I'm gonna ignore this part and we'll just keep the rest. And I'll put here idea of one, one, one. And then we'll put here Harry Potter and H Potter at, I don't know, wizard3.com. And then gender is a male and IP address is just like, whatever, right? So when I create the object or the data and then we'll go into a const result is await and we'll do a student that insert one, okay? Insert, should be one of this. Now we used to insert one and then just pass in the data which is this data here, okay? And if it's successful, we're gonna send the result back. And the result will be the default result, whatever MongoDB returns, which is an object of some information, okay? So we'll do that using the post. And since I can't not test it here, I'm gonna launch postman. So here's postman. And then the IP address for this is, I think is at 9,000, okay? So 9,000, here we go, we get and we get the message hi here. So if I go 9,000-students, you get all the students here, okay? So this is the API tool for testing your corporations. So here we go, we get a hundred of them. It tells you somewhere, I'm pretty sure. But we get all the data here, okay? So now I'm going to copy one of these. Actually, I'm gonna go to the post. We're gonna go inside your post here, the body. I put something here, you can ignore this for now. I can leave it blank, it doesn't matter because what I'm doing is that when I run the post, I'm going to insert the data using what I have here, right? This isn't accurate, so it doesn't matter. I'm gonna insert that to the database, okay? So let's see if this works. So I'm gonna go ahead and then click send, okay? So I got an error, same something is not correct. If I get this kind of message, something is wrong, right? Otherwise you get a better response. So if you're not sure what it is, you can click the view in the console here. It will tell you what the error is. It says something like, let's see, read reset or something, not sure why. This is not the helpful, I guess. And let's try again, let's do over here. And let's see if it tells it anything down here, which is sure down the bottom. It says insert one is not a function. Okay, so insert one is not part of, I guess we're gonna put a mongoose. Interesting, that's all it is. So that means we can't use insert one. So we use insert many, I guess. Let's try that. So let's save that, go back to the Postman and try again. Okay, so let's close this and just send. There we go. So we got the message back, we got the same object back. So the result is that maybe MongoDB sends the same object that you created. You just add it, it sends it back to you. Okay, so we can verify this as well in MongoDB. So let's go over here and check that we should have 103, I mean, four, if I refresh this. So we get 104. Now to go to the last one is keep, just keep going all the way to the end. Here we go to the last one. And here is the Harry Potter object we just created. Okay, now remember that when I made this, I had this object in the code. You know, I just manually put it here, right? I just manually put it here. If I run it again, okay, because the student object uses a schema, this schema says the ID is unique. Okay, that means I cannot have duplicates. Same thing with the email address. So if I go ahead and do it again, this should fail. It should not re-insert this data. And we can give it a try again, go into postman and just click up post again. Okay, you got a message back, but we'll see if it's still in the database. So we should have, if it's correct, we should not get 105, but it did add 105. So I think it probably did not follow my rule. Yeah, I have a duplicate here, right? So somehow this didn't work for some reason. But let's see if we can do, student insert me. Yeah, I'm not sure why it's not. Hmm, interesting. Okay, yeah, I have to figure out. Usually this is, it should not let you insert duplicate data. Okay, but this time I'm gonna do this way. I'm gonna go ahead and not use this. I'm gonna get this from the front end. So you put here const data still. So we're coming from the request body, and they wanna insert that data here. Okay, so I'll clean this up a little bit. That is for the post. And then for the other ones, like the delete and update stuff, I'm gonna go and just copy this. And I'm here, see all some typing. And this is gonna be for the put. What do we need for the put? We need the ID and the payload. So the payload will be same as this. Copy this, put here. I need the ID. So the ID will be, I need to convert that to a number. The response.params.id, okay? And then when you do a update, you wanna call the update function. As you can see, update, mini update one. Can you update one? And the inside here, you have two things. One is the filter. The second is a set key, like that. And you pass in an object, okay? This is the setup for that. So you cut the update one. What are we updating? Here is based on what? We're based on the ID. So you put here, the ID is equal to the ID. So just make sure that we do call it ID, okay? So if this is ID, ID, the same key and value, then typically you just call it ID. Save your typing, okay? Just in the nature of object in JavaScript. So this is same as saying ID of ID. Okay, repeating. And then for the set, what are we changing? Are we changing only a certain field? Or are we changing the entire object? Doesn't tie up to you. If you change everything, then you pass in the whole object. Like the data here will be the whole object that you put here, data, all right? If I'm just changing like the first name, then you have to put here the field, first name, okay? Like the name, and then you pass in the new data, would be data. And then here you will have to make sure you get only the first name, right? Or I should call it first name here. Like this, right? First name and then first name. So again, same rule. If it's first name, first name, just first name, okay? So I'm up to only one field. If you update only certain fields, you do that. Easiest way is of course, you just replace the whole thing, right? So that means just replace everything, call it data, and then just replace the whole thing as data. And then you don't have to put it in encoded braces because it's already encoded brace. So I'm replacing the entire object, okay? So that is for the put, okay? You can do many or you can do one. If you put many, then you want to put a different API endpoint, right? And then down here will be the delete one. So let's put here, very simple. This is the delete. We delete one. So again, we need that ID for the body. We don't care. And then the delete would be just delete one and pass in just the ID matching that ID. Then we are good to go. That is the delete. I missed there. Here. And then down here as well. And then finally, we do the delete all. This is the most dangerous one. We don't need the ID here. This could wrap into the student's address. And then down here, I don't need the ID either. And all I do is delete. You can put delete many here. Then you pass in a blank object or just leave it blank like that. This is, it's gonna wipe out everything, okay? So this is the very dangerous one. There's no undo either. You can pass it here. You can also pass in here the object of, like I said, I want to delete all the, maybe all the gender, if I have a gender. Of all the male, if I do that, then it's gonna delete all the records that have a gender of male only, right? So you can filter that many. How if you put one like this, delete one, if you do that, then it will only find the first occurrence and it's gonna delete that one and then it stops, okay? So just something to think about. We don't want, but we do want to delete everything. So we're gonna put empty object here and then delete everything, okay? So that is it. Let's go ahead and give it a test. Now, before I do that, I want to make sure I have a copy of my data first. So just to be safe, right? I'm gonna export this out and I'm gonna save to the full collection out all the fields and then save it as adjacent data and I'll put it inside just on the desktop for now, okay? Student object, save it here. Just in case I, if I lose it, okay? All right, so now let's go ahead and verify our data. Let's go to the postman. And again, if I post another student here, I'm gonna get, so now you can see that it has some error, right? What is the error? Well, let's go figure it out here. Now we say, now here it is, ID is required. Hmm, I forgot something there. For the post, I don't know why these are required here. Oh, because I didn't put any record here. I see it's an empty object, okay? So let's make a copy of that object earlier. So this guy right here, right? Undo this, I'm gonna cut this out, save this one. And then I go over here, put it into the body and then make this a little bit bigger and we don't need this whole thing here actually. Yeah, so that is the data. It is gonna be JSON data. And then I'm gonna add that to the database to see if this works. Boom, and there it is. We have a data coming back saying we add it. Again, it did not follow that unique key thing. It should have, for some reason, it's not working. I think we have to verify. So add another one. Let's put, you know, just a number. I know the name is the same, but let's put here Larry instead. And I'm gonna add and boom, okay? We get that one there and then we can do a return. Get the ID of that student call 1212 ID 1212 and we got it back, right? And then we can verify as well. Make sure it's there. It should be there. We have 105, now we have 107. Again, all the way to the end. It won't let me do all of them. So here we have the last two, 1212 and 1111. All right. And we have three of them, right? We have three, three 111s. So we're gonna delete those three 111s. So let's go and delete them. So go to the delete. I'm gonna pass in here 111, okay? Delete and we've got an error. So let's see what's going on. Again, we're not sure. Go back to your console and look and see what it says. It says, ID is not found here. Oh, it's rec, not rec. So what I put, it should be rec. So that's rec. Up here, it should also be rec. And okay, okay. So let's try again. All right, so let's go and delete 111. Boom. Okay, we got it. It says, technology is true. We got one deleted. Now we can try to get it back. It should not be there. We got it. We do have it because we have a couple of them, right? So let's keep deleting those until they're all done, okay? Delete, just really one. Again, again, and then now zero, so there's no more, right? So now you get it back. You should not get that anymore, okay? And then we can verify and the database just refresh it. You see that it goes back to 104. It should not be there anymore at the end of that. See there's no more Harry Potter here, okay? So that works just fine. Now the next one here is the delete all. This guy right here, the dangerous one. So remove that ID number, go to the delete. And here we go, boom. Okay, 104 of them deleted. And if you try to delete again, you're gonna get zero. That is no more to delete, okay? You can confirm getting the data back. It's not there anymore. And we can verify in the database as well. There's none in here anymore. Oh God. Hey, perfect, right? So there you go. That is how you do these operations using mongoose. As you can see, it looks very similar to the MongoDB library. The only difference is that you set your schema and you create your models and then you make the connections. So all these functions are similar to the MongoDB collection library here, right? It should be the same, right? So I hope this is helpful. If you have any questions, again, please put in the comment or just let me know and I'll be happy to help. Thank you so much.