Configuring the backend Section 5. In this section, we'll continue working with the CRUD operations by modifying all the APIs to interact directly with MongoDB, we'll be using Mongoose and its functions to perform all these CRUD operations. Querying documents using find. We'll start off in this section, in this video by making queries using the find function. This is a Mongoose function that mirrors the underlying MongoDB's find a function for fetching data. Here is the syntax for the find function. You would pass in a query. The query is the filter, which is a document of filters, and the projection is optional just for returning any field types, you want to return. This function returns an array of documents. Here is a list of some statements in MongoDB and how they will be translated to statement in SQL Server. Feel free to pause the video to review them and see the similarities. Let's go over to Visual Studio Code and create our read operations. Let's go to the backend. In the previous video, we modify our APIs folder and we added it into the app.js file this input here. I'm going to remove this. Well, first of all, notice that we do not add any data to the database yet, so if you go to Compass, there's nothing here. We don't see it because there's no data. Why don't we go ahead and just create one very simple document here first to populate the data. Go to the database file in the line here that I use the provider. We'll create one very quick object in here. Because we're using the data here, it needs to follow this schema. That means we have to go into the models and grab one copy like this first one here without the ID. I'm going to copy this and paste it right in here. You should pass all the validations here. Let's save this and go to the terminal and run our app. Successful. Now, this should already be created. Let's go to the MongoDB Compass and refresh our database. You see that now it's created here, providers here, and we have one column document, and it's now working. We're good here. Now, since this is visible, now we can go ahead and delete this for now. I'm going to import all the data from the provider JSON file to this collection. Import the file inside the source data file, the new provider's JSON file. This is the one we want to get, this is the same copy of the data we saw in the code, except now we have removed all the IDs and it just contains all the fields. We ignore that and stop on errors just in case, import, and now we have all the data here for us to work with, so 20 records. We can now go back to the code and I can remove this now, I don't need this anymore. Let me just turn this off. Then also in the app.js file, I'm going to remove this. I don't need this anymore. We're done here. We're good. I must add one more thing to this file here. This is like the route. I'm going to get one more just in case, if you ever go to a URL, you're going to go to a certain page, so you put a star here that's like a wild card, and that will take you to the homepage every time. Just a safeguard thing, so we'll save that. This one is done. Now, I'm done with the DB also, I don't need that anymore. We need to go into just the controller here. Where all the events occurred. We're not going to use this anymore. This is coming from just the object file. I realize I have a typo here. This is like misspelled, th. All this time. This is not needed, but we'll leave this for now. Everything will be managed in the MongoDB. We want to make sure we can check for the objects and keep that. Existing providers, we don't need that anymore, so I can comment this out. The unique ID generation, we don't need that either because MongoDB would do that for us. Will prevent that from being created and I think that's good to go yeah. The rest will be good to go. Now we need to import the db here. We're going to have constant db, we are going to call it provider, the same name I had in the other file. This would be coming from the db folder and then the db file. This is the provider will allow us to do creation and things that. We'll keep that. Let's go down here to the create. We'll do that later. We're going to do the read. Here's the read, we do readAll and readOne. Right in here, we check to see if the provider is empty and now we leave that. Here want to do a readAll so we'll do provider.find(), use the find function. If it can return a promise then we set.then, data results comes to that variable and then we'll do a call here inside here, if it's successful. If it's not, they want to catch the error here and we'll do something that then log the error that. This whole thing is going to go inside the successful part in here that. Now, we also want to catch it. It's always safe to try-catch to handle any uncaught error that not been caught there and we'll do that as well. Catch the error here. We're just going to log that to the console. We can also show differently, but for now will be fine. Say error. I realized that we're going to have a lot of these errors and so on. We're going to create a function up here to make it more feasible. We have a function here to handle all the errors. We say handle errors and function handle error. We're going to pass to this the, because we're going to send that over to the API without showing this to the browser. We're actually sending a copy of this an error, you put that in here, in the function. That means I need to get the RES object and also the error message. We'll return not the provider but the error. We'll put a message we see down here says "Something wrong. Will say "Something went wrong. " Put the slash n for new line and then we'll just plus the error message to go out there. We'll do that they're and so we can say handle error. We pass that to this part here on the catch. Put that here, and will also put that here as well. Now the fun part, and let me format this. The fun part is when we find we're we looking for, where we find everything is not matching so you can leave it blank like that or you can pull the empty set this it's fine too. That's totally cool. We can just leave it that it's, and finds everything if it's successful, then all the data will become was stored in this result as an array and then we're going to check if it's empty or not. If it is none, there will be zero and we say the list is empty. Otherwise, we successfully, we treat that and we turn the result, not the providers anymore, the result to who'll request that data. Let me save this and we'll just test this part to make sure it works. The other one will be still calling from the object file so that should be. Let's save that and now we need to fire up the Postman. We use Postman to check our API. Made the connection here and it's going to be coming from local host port 27017 and API slash providers. We're going to do a GET, if it's successful, we're going to get some data and something's wrong. Let's see why. I think the port is now ready. That's the database port. I think it's 3,000 that's going to be used. Let's try again. It works. Here we go, these are all the data that comes through. This will be 20 of them. You can count them, you can write a code to the test in here to show the code bar, we had 20 of them. That works if I want to select only one from the ID. Now we put that here and we're going to fetch that from the database. Here, nothing because we did not code that. Let's go in and do the other one. We got that part done. Now let's work with the GET ONE, similar to this one here. We are first going to go in here. We get one only. We need the ID, right? This part, we need that, the ID field also need that. Now, this one we don't need. This can be commented out. We're going to get the ID. Let's put that above here. That's the first thing we want to do. Check for the ID. Because our ID is an object ID, not a string. We have to convert this to an object ID. That means I have to go to the very top here. Let me maximize the screen. I need to import in here the object ID from the mongodb. It will convert that to the actual ID so we can compare in our code. Otherwise, it won't work. Right down here, we need to cast that to object ID. Once we get that, then we can compare in our search. But again, it's the try-catch thing. So try and it will catch it down here. I'm going to copy what I have up here. Put it right down here at the very bottom, and I'll clean up in a little bit. This is where we do the search, right here will be Provider.find again. Then the result comes in, if it's successful. Then they also catch the error. I'm going to again copy the catch error up here. This one goes right in here. Then all these will go right inside this part here. I'm going to copy all of these lines, 90-97, and drag it inside the 87. Do a right-click and do a format. There we go, everything's cleaned up now and we are good to go. Now, the find. This time we're going to match the ID. This ID here. We do a filter here. We have the ID field, the ID field in the list, and the database. You can use the '_id' matches this ID here. If it matches, then return that document only. If you don't put any at their options, the whole thing will be returned into the result and we check the result here, see if it's empty or not. If it's not empty, then we're good. Go ahead and send that data over. We're going to send that result over, not the provider. Otherwise, let's say list is empty and nothing is read. Let's save that and go over to our postmen and this time try it again, the same ID here. Run it and there we go. We got the ID back and it's successfully working. Again, if I take that out and if I try to get all of them, okay, good. If I try this ID here and get that ID back, looks good. If I try a different ID, let's say put a B here. Now you get the message because you said something went wrong and the error is you have to use a 12 bytes string or a 24 character hex character. So all these have to be 24 characters. So it's going to cool here. If I put something else, like a C or something, there's nothing there. Then it doesn't match for some reason, good guess. What about x? There's no data and I put probably too many letter here. That is good to go and we are good here in this part. In the next video, we'll insert documents into the database using the create method.