 Connecting to MongoDB. In this video, we'll modify our app to make a database connection to MongoDB. We'll be using an object data model or ODM program called Mongoose. Now, using Mongoose, we'll be able to create schemas and models that will work flawlessly with MongoDB in a synchronous environment. We'll also install another program called Nodemon for monitoring changes in our code so we don't have to restart a server each time. Here is how you can install the drivers, very simple. Once we install that, we can go into the code and import it, create a connection, and create models and schemas for our program. So let's go and do that right now. All right, so here in the IDE, I'm going to go into the terminal and do the installation. We'll do MPM install. We'll do both the Mongoose and MongoDB. This should be fairly quick, small module. Okay, so we also want to install the other one, the Nodemon. And this one is going to make it global installation so it will be available globally throughout the program. All right, so I'm going to go and open the back end folder here, and let me just close this for now. So we only want to modify the API, so go into here and let's create maybe two folders. One for the database, I just call it DB, and I have another one. I'm going to call this is the schemas. Now our program is really small, so you may not need it, but I'm doing this for a practical reason. And the idea is you want to separate all your data in a separate environment. So you have your controls here, your database here, your models, your routes and schemas and so on. Okay, so first let's just go into the database and make the connection. So I'm going to create a file. I'll just call the same name, db.js. Inside here, then we're going to import the Mongoose. Okay, so once we create that, then I'm going to go and create a URL. This is the, let me put a comment here, URL your eye for MongoDB. And this would be your eye, the string MongoDB colon slash slash. A local host, or you can use the IP address is sometimes safer to use the IP address. Port number 27017, and I'm going to assign the database right away. I know we haven't created it yet, but I'm going to create a database called provider underscore db. Now what this will do is once you make the connection, then it will create that database automatically if it's not there. Okay, if it's there, it will use it. If it's not, it will create one for you right behind the scene, which is pretty cool. So we get that done. And now we just have to make the connection. So I'm going to say Mongoose.connect. As you can see, it takes the URL and option and a callback function because all these are done asynchronously. So the URL is this one here, and I'll come back to the other options for now. And you can use a callback function or you can use the then function, either one is fine. It returns a result, which is the connection itself. And then we'll do a console log to show that this is a successful connection, if it is, right? Success. And then we also maybe want to log the results, see what that is. And if it's not the case, then we want to catch the error. And we also want to console log this to the console, so error. So we can see what those errors are if there's any. Okay, so that is our connection. Now notice I did not assign this to a variable you can if you want to. Since we're only making one connection, it's going to be assigned to the Mongoose connection. Then that's all you need. If you have multiple connections, if you want to make it, then you can set it to a variable, like const and then you know, account one equals that. And you have another connection, you make another one and so forth. Okay, so ours is just a single connection, so we're good here. Now, for testing purposes, I'm just going to export this out. But firstly, I'm going to put a comment here, make, say, say, connect to MongoDB. I put here asynchronous. This is asynchronous call. Actually, all of these function calls are asynchronous. Okay, so I'm going to just basically export this out so we can just test the connection. Okay, so let's save this and open the app.js file. And I'll just do right here above the app just to test it. Well, we can do right below here. It doesn't really matter where, but right here it's fine. So I want to do a constant DB connection. I'm going to import the DB here inside the, no, the DB or API, DB and then DB. Okay, so once we call that, it should run and then we will have the information here in the console. So let's save that and let me navigate over here and see what that, we can make some changes. So go to the terminal and now clear my console and we'll do our usual MPM start. So here we go. We ran and we have some information back. So let me maximize this. If I scroll up, you see all this information is the result. That was returned by the connection. And here's our message here, the success message. But after that, we have the actual string from the result. It is a connection that gives all this information here. Somewhere down here you see we have a name of our database called provider underscore DB. At this port number, this holds and so on. We didn't have a user and password here. If you have it, you want to supply those as well. The Mongo client, it's the default client and our models. You don't have any yet. We're going to create one eventually. But for now, so it shows, if you see this, that means we're good to go. Okay, now up here we have two warning messages. These are deprecation warning messages. That means that some of these code are out of date. So we want to suppress those errors. That is warning. So what you can do is it gives you an instruction here, say to use this parser, put this option in the Mongo client connection. So we're going to basically copy this and let me minimize this again. You can put that here right next to the URI. It takes another option here. And this is the options here. So you can add that here. The second warning is this used apology. True here, put that here as well. Put a comma and then put the next property there. So if you do that, and that should take care of the problem here. And then notice again that I have to, once I make the changes, I have to terminate my program and install it over again. That's why I installed NoMan. So NoMan, a monitor, will monitor that for us and it will keep that active. So we already got the results back. If I scroll up, you see that the error messages go away. We made that call right here. The messages are gone and we have the success plus the result. If I make any changes in here, if I make some changes, say, let me go to the bottom here. Say that I make a change in the code, just a space, and if I go back and save, you see that it renders the whole thing and runs the whole app again. So I don't have to restart again because that's pretty nice. So you can do that here. So we're good here for this part. I'm going to go and turn this result off. I don't want to see that in the console every time. Success, we can leave it now. Later on, we can turn this off, but for now, we'll leave it as this for now. Okay, so next, let's go and create our schema. So schema, I'm going to have another file. I'll just call it schema as well. The schemas are the structure of our documents. So we're going to follow the same idea in the models folder. If you go into the models here, if you look in here, this is our schema, the shape of our document. So I'm going to just copy this and I can close this. Now I paste it over here for now. I'm going to do something in there. So we want to import the mongoose as well. So const mongoose, require mongoose, oops. Okay, and then here we're going to have a schema. We're going to define a schema, just an object. So constant schema. The reason why I put a capital letter is because it's a class and we're going to instantiate that schema. You can put it here. So I'm just basically re-assigning this to the word schema. Otherwise, you have to use the whole word again each time. So just make it a little bit shorter. Okay, so once we get that, then I'm going to make this schema. This schema is like a data model, right? The model of your model, object. And so this is the whole provider schema. So I'm going to make a variable here called, like I say, create provider schema. I'll put a comment here. This is the parent schema. And I'll show you why in a minute. So I'm going to say constant provider schema is equal to new schema. We call that schema func class. And then we're going to pass to it an object. Okay, so this object is this one right here. So all of these, we'll put that inside that object. And then we need to change all of these to types. Okay, as you can see, it's already in JSON, which is good. We just have to change this to, this would be of type, we're going to remove the ID. So I'm going to let MongoDB manage ID for us. So we're not going to use the regular ID here. And we'll be using the underscore ID. And since the IDs are not really that important or useful for us here, we're just going to let MongoDB manage that for us. So that means you can omit, at least we got this ID field. It will be added automatically. It's like the primary key. Now for the first name, you want this to be of type string. Okay, you can put the string here as a type, or technically the correct way is to say type. And then you put string like that. Okay, that means it takes a type of string and also take the type of string and so forth. If you just want variable like this, then you can just say string and you'd have to put the word type there. But just be careful because a type is a keyword. And if you have like a variable here type, what type of user is this? If you put something like that, then it's going to think that this is the data type for whatever it was before. So be really careful when you do this. And let's say that you can also add validations in here. So it's a type string that is part of the validation. You can say I want this to be required. So if you say required is true, then that's to be true. You can also set like minimum maximum, the min and then like min has like two characters and max would be 20 characters like that. You can put all those there for validation purposes. So ours, I'm just going to say this is required and it's a string. And let me copy this and put it to this as well. So that will be required to position. This will be optional. So if it's just optional, I'll just use the short annotation for string. Okay, the company name, this is, maybe we'll make that as a required. And the address, yeah, this is required. I have too many commas here. The address two is optional. So we put string, the city, yes, required, state required. And here you could put, if it's just a state, you can put here the maximum. Like the minimum will be, you know, two characters, max will be two, right? So it's the same. And then the zip code is required. I put here, well, you don't have to put max min, but maybe like min will be at least five digits. The type will be string, but I will put digits anyway, strings in a way. Email will be, phone will be optional. Well, I'll say we require that, at least one phone number. And then email will be also required. And the description could be optional. And so is the tag line. So we made those optional. At least the type of string. Okay, so that is our provider schema. Now, the reason why I put the parent schema here is because, as you can see, I mentioned in the other video, is that every time you see a pair of curly braces, that is a document. So here I have a sub document, or this is a child document. You can leave it as is for now, which is fine. But the best way to do is to break this out into its own schema. So you want to go up here and say create the company schema. It help put the embedded, or I guess called the sub or child document. And I have here called company schema and equals to new schema. And the reason why you do this is because of, you know, reusability, right? You want to reuse this again, maybe just in case you have to. So I'm going to copy this whole part and put it right in here. And then here it's place. I'm just going to replace that with the company schema. Okay, so now you, now I can reuse this. Now it's important that you take care of the order here. So because I'm using this right here, this schema has already been existed above. So I can't put this below down here or it won't work. Okay, so the order is important here. Okay, let's see if this works. So once I'm done, I'm going to go ahead and save that. And then I'm going to export these out to where it has been used. So I'm going to have a module export. And I can export both of them as an object. And the reason why I do object is because you have objects, you have access to each of these properties, right? If you want to use them separately. If you just say, you know, provider schema, you can just do once. What if I want to reuse the other, you know, company, or if I create more schemas, what I do, right? So you put the object, it's always better that way. And you can access the whole list. So I'm going to send both of them out. I mean, we not use it, but just for good practice. All right. So let's save that. And that is our schema. So I think these, you don't have to be used like quotation mods, but I'm going to leave it as is for now. And so we're done for this one. And then we're going to export these out or input them into the model. So now we're going to, we're not going to use this one. This is just a, an object, right? We're going to create another file here. And I'll call it, not to confuse with the provider's model. I guess I'll just call it provider. Okay. And it's already inside the model. So we'll, I guess it's understood. So in here, we are going to do something similar. We're going to import the mongoose require goose. And notice I did not import mongoDB at all because the mongoose has already used that. And then in here, I also want to import this schema. So const and the schema, remember there are two of them, right? So I just want the provider schema. And that is coming from the models, not the schema schema. And this is the reason why I said you want to pass as an object because I can pick and choose what to import, right? If you want to use both of them, you import both of them there. Otherwise you're good here. So then now I'm going to have a provider. So I'm going to say create a provider model. This is the model here. So I'm going to have a constant. And I'm going to make this a capital letter. It appears just because it's a model. And I'll be going to the mongoose function called model. And then here you pass the name of the collection. Ours, I'm going to call it providers. If you put it here, if there is no such collection, then just like the database, MongoDB will create that for you automatically. So the first one is a string we're going to call this provider. Now, typically, if you think about table names and collection names, you can kind of debate this whether you want to make it plural or singular. So for MongoDB, if you use mongoose, mongoose will always pluralize this for you. That means if you provide the name as provider and then in the database, mongoose will create a collection call with a lowercase p, all over case, and with the letter s to make it plural for you by default. If you provide providers, then it's going to keep that already because it has an s. So it's difficult to put this provider here, and then you leave that to mongoose to do for you. So that is the name of the collection. And then here is the schema. This is why we imported the schema here. So you can also put all your schemas here in the same file, but again, I want to separate all of those into their own areas. So just input, export them out. So this is the provider schema you want to use for that model. And then now we are good here. So after that, we want to now just export this out to somewhere else. So I want to export the provider here. You can also use the object if you want to. Again, why? Because again, I might have up here, I want to use the company schema as well. And I might create another constant, maybe this is the company model. And I'll use a different collection, maybe company. And then I use that as well for the company schema. And I also want to export that out. So in the same provider, well, usually you would call this like schemas, I guess models, but I can do both of them. And so it just makes more sense to use as an object instead of a singular provider. So I'll keep that as is. I'm going to show you how, why I chose that way. It's mainly just a design preference. I'll use it this way. So I'm not going to use that. It's going to turn this off. I should you want to modify this app and make changes, then that's the way to go. So I exported that out. And then now, I think I'm pretty much done here. And then finally, we're going to input this into the database. And we're going to re-export that. The reason why is because I want to call this DB, not the schema or the provider file. Once I call the DB, then it's going to do the connection for me and also create the providers, everything for me in one go. So up here, I'm going to import the provider. So it's a, because I used object require. And then here will be coming from the models and then the provider. And then I'm going to export the provider out. I no longer need the module, the connection here. Okay, because this again, this one connection. Once you invoke this, it's going to make the connection and the connection will persist. Okay, so I'm going to turn this off and we'll remove the export. And instead of that, I'm going to export this model. Because this model now, this provider is actually a model. If you want to call the provider model, it makes more sense to you. That's fine because all it is is now, once you call mongoose.model, this object will allow you to find all the credit operations through this object and not through this one, right? I mean, not through the connection, through this model here. So I'm going to import it here and then I make the connection. And then down here, I want to export that out as well. So I'm going to export the provider. In this case, I'm just going to just use one. Okay, and so we're good here and kind of show you what I mean by provider is so once you make the connection down here, you can say, you can do a creation, right? So you can say provider. Well, let's see, we know that we created the console, we created the database already. It's been connected. And although we don't have anything in the compass, you have to go to the compass to show you. So over here, you see that if you refresh it, the database is not here because you don't have the data yet and there's no collection yet, right? So when we run this app, actually the collection is there and just there's no data. Okay, so if I go in here in the terminal, let me open another tab here and let me just go into mongoose, I mean mongo shell. And down here say db show dbs. You see it's not there, but our database is called provider db. Okay, and we already created in our provider a provider called providers collection. So if I switch that over to the provider db, you see that that's been switched over. And let me clear this. So db is provider db. If I do a show collections, do I mis-type that? Let me see if I did. Or do we call this? Oh, yeah, we didn't invoke this yet. Let's see, what did we? Yeah, I did the db from here to the db here, which I exported the provider. So yeah, it should be there. Let's see. It shows that there. Yeah, it's not there. Maybe because there's no data there. I'm just going to go and see if it has some data in there. Okay, so let's just say that here I'm going to go into what is simple function called create. And I'm going to create a just an empty object or data in there. I save that and make sure it ran already. So it has some error. Is it successful here? But it has some errors in here. So maybe that's why. Provider validation. Oh, yeah, because I did not follow my schema. This is a fail validation. My provider last name, first name and are required. So it does work. It is there. Otherwise, you will not see this. And we haven't created any data yet, but it's in there already. So that's good. So to test this, I can go back to the providers list and we'll just copy one of these guys. We'll copy the first one. Without the idea, I'm going to copy this. And I'll paste it into the code here just for simplicity sake. Put it in here and remove this. I don't need the idea and that part here also don't need the extra colleagues down here either. So this is my schema, right? I got to match this. Let me save this and see if that works. So down here at the bottom, you can see that it successfully added. I can now go to mongocompass to see if it's there. We refresh that so you can see we have a provider DB, our provider's collection, and we added one data here. So that is working nicely and we are good to go here. This is the end of this section. Now that we have successfully connected the mongodb in the next section, we will configure and update our APIs to interact with mongodb.