FastAPI MongoDB REST API w/ Python and PyMongo | CRUD Operations mongodb | Swagger UI |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to my channel so in today's tutorial i'm going to go over a highly requested video from you guys so in today's tutorial we're going to go ahead and actually build uh a we're just going to build a simple api back-end using fast api and mongodb as a database so in the last previous time i have made some a whole playlist on fast api and react so it's uh it's just a full stack application using first api back end and react as a front and so i in that in that series i use uh tortoise orem as the back end so you guys some of you guys requested that i did uh a farm stack a complete firm stack tutorial meaning that first api uh mongodb and react so that's the farm stack right first apa react in mongodb so you can think of this video as an entry point into that direction so it's going to go ahead and actually build a simple to do uh to do api using first api and uh mongodb and then if you guys want you can drop a comment the section below we can uh wrap a front end around this using react and then get the full stack application which is which is just going to complete the farmstack uh thumbstuck uh uh framework right right like the stack the technology stack for the farmstack so yeah so even if i don't do that right i'm going to teach you how to use mongodb and you can watch my previous first uh first api in react series and then you can connect the two knowledges together and then you'll be able to implement a farmstack uh application using a first api mongodb and the react as the front end so let's get started so uh the project is going to be a very simple project so this video is going to be uh one but this one video is not going to be any series or anything like that it's just going to be a simple video uh integrating first api and mongodb so let's get started so the first thing you need to do let me just tell some of the prerequisites is that uh you need to have um you need to have a basic understanding of fast api because i'm not going to explain the basic concepts in depth but even if you don't know first api like the basics feel free to follow along i'll also explain the i'll try to explain uh to go a bit in depth about the basic you also must have python installed obviously and i'll also need to have an account open on the cloud.mongodb.com so you can just go to cloud.mongotb.com and then you can just open up an account there and then you'll be able to log into this place i'll put the link in the description and other details on how to access this web web platform so once once you have this uh you have created an account here we'll go ahead and actually create a cluster so i've already created a project and instead of the project i'm going to go and actually create a database so i'm just going to click on this and i'm going to go and choose the free one because i don't want to pay for this so if they have more than one uh database credit then it will be required to pay for it since i already had a database that i was using for an express project but i deleted it uh and then uh start you don't you don't have to pay to create anyone so you guys can have the whole experience of creating the database yourself and then getting familiar with the cloud.mongodb.com platform okay so i'm just going to go and actually create that and this is going to go ahead and bring me a page and in this page i'm going to put in some uh some values here so like where i want the server to be and other stuff like this so you can see it's completely free but if you already have a database running on this this platform then you have to pay for an additional one okay so i'm just going to go with amazon azure and i'm going to code with india since it's closest to my location and i'm going to go and actually go ahead and uh and go ahead and actually create that so i'm just going to go ahead and actually click on the create okay create cluster and it's going to go and actually create that cluster for me so this might take some time to create the cluster so i'll be back i can see that back so you can see now my cluster has been created it takes a bit of time so that's why i had to cut the video and uh resume whenever it was uh done so this our cluster is not being created and it's an absolutely free so if whenever you want to create a new cluster then you have to pay for anyone okay but this in this project we'll be using only this cluster so i'm just going to go ahead and click on the connection on the connect rather the connect button and then it's going to prop up this page so in this page right i'm going to go ahead and actually say i have three options connect with the mongodb shell uh which we are not interested interested in the mongodb shell and then the second is connect your your application which you're interested in and also you're not interested in comma connecting into a mongodb compass which is just like the mysql workbench for mongodb okay so i'm just going to go ahead and actually click click on the connect your application because that's what you want to do this is going to bring you to this page by default it's not it might be another language so make sure that you select python and then you also set the python version i want 3.6 or later so i'm just going to put that option there and then you can see that replace the this with the password and then replace this with the name of the database that the connection will be uh will use to by default okay so i'm just going to keep this keep make sure you keep this in mind okay so you just copy this now which password do you use so use the password of the user who manages this system so for example if i go back to here database access then i can see the person who has access to the database and i can also change the password of that user so you can see that this is a person who has access to the database and my uh my name is prince here and i can also edit this detail i don't want to show you my details i don't know if it shows the password so i just want to be safe so in your case you can click on this and then edit it and also change the password in case you don't know what the password is okay so great so make sure that you copy this right there so make sure that you have this and we'll be back to this right it'll just in a second but before we continue we just mongodb now that we have our database setup let's go ahead and actually install the the libraries and everything that we need okay so i've already prepared this document on my github page so i'm going to drop this link it was already completed project i'm going to drop it uh in the description so you guys can find this so i already wrote some mark down here a bit detailed and explains everything so we need to install a couple of libraries and dependencies that our project needs so we need to install first api we need to install uv con for our server and then you'll see we also need to install pi by pi which is just um the the library to work with mongodb databases in python so pymongo is just a library to work with uh mongodb databases in python also for for the pi we also need to install pymongo svr the reason why we're installing svr i'll just bring you back here and now go to uh databases and then i'll simply try to connect bring click on the connect button again and then i'll click on application you can see in here you're having this svsv uh sorry srv and the srv that's why we need to install this if you don't install this thing if you don't uh put this in uh you'll get an error whenever you're trying to connect your my um your mongodb database okay so make sure that you have that installed so i'm simply going to copy this and then i'm going to go into my team and also i'm going to bring up my terminal this way and then i'm going to make a directory on my desktop and i'm going to say we're going to simply going to call it first uh api make sure i get that right api okay db something like that okay and you can see we have that folder right there and i'm going to simply change into that folder and then right there so i'm going to create a virtual environment right right now i actually have a virtual environment running so let me just deactivate that one then let's create a virtual environment from scratch okay so i'm going to go ahead and say python and then uh dash m v env v and v okay so i'm creating a virtual environment called venv again you can change this can be anything you want but the rest part of it must be as it is if you're on linux you can also use p3 uh python 3 if you want but it's also fine so uh if you're on mac uh mac and linux make sure you keep your patent 3 in case you don't have uh you didn't set up your your bash rc file okay so i'm just going to press enter and it's going to go ahead and actually create for me that's a virtual environment called venv so when i do an ls i can see i have a folder now called venv and this would virtual environment so let's go and act ahead and activate it so to activate it is very simple just have to type in the following command so you have to say source venv which is the name of the of this folder and then in there we have a folder called bin and in there we have a folder called activate so just type that in and you can see our virtual environment is now activated and you can notice that because our name of our virtual environment is in parent uh is within parentheses in the front of the line within our terminal so great so now that we have that uh uh uh that virtual environment set up let's go ahead and actually paste the the code that we've just copied from our github and then we'll go ahead and actually install this so i'm just going to go ahead and actually say uh pip3 uh install install uh first api you want to install uh you vcon you want to install pi and usually supply um and then the square brackets and then um in here i want to want to type in um you want to type in s srv okay so make sure we got that right so you can also the reason why we got an error here because i didn't i forgot to remove this one that's why we got the command not found okay because the dollar sign is not a command linux so you don't have to say pip or install fast api you've gone pi pi is uh using a square bracket srv okay make sure you run that and this is going to actually install all our dependencies for us so great so now that's installing let me just bring you back to our github page so now that that is installed we also need to create a project for a project directory like this so we need to have folder called uh config we're going to keep all our configurations including our database connection configurations we're going to have a model so we're going to actually define our models like the identity model that we'll be using in this tutorial and then we have routes we're going to have our different routes and then we have schema however we're going to keep our schema our schemas basically as just like serializers if you come from django background okay so you just have to represent the data in a certain format that you want okay great so let me just go back to our terminal and make sure that we have all that installed and everything went fine so great so i'm just going to clear the terminal and i'm going to open my visual studio code or feel free to use any editor of your choice and we're just going to go with visual studio code again feel free to use any editor of your choice okay of your liking okay great so now that you're here let's go and actually create the therefore they predict their uh structure so we need a default folder called config and then um we also need another folder called uh mts go back so we need a folder called config um just okay here in the folder called config you know called models routes and then schemas okay so let's go ahead and actually create all those tables so we need another uh sorry folder called uh it's gonna be routes and then okay sorry it's got created in this let me just delete it uh sorry about that so let me just delete it and then i'll create a new one in the root directory so in the root directory let me make sure that i'm in the root directory and then i'm going to create one called routes and then i'm going to make sure i'm getting a root directory create another one called uh schemas so uh schemas you can type schemas okay schemas then let me create another one in the root directory called uh modules a pdf so it's gonna be called modules okay we're going to keep all those so great now that you have the let's go go ahead and dish will you take you back to my github you also need to have one uh one one python file root directory called main okay or main or index.phi so feel free to call it a domain or index whatever you want okay so i'm going to constantly call it my main dot pi okay yeah but they stick with convention and call it main or index okay now that we have that uh we are good to go so i'm just going to start a basic of last ipad so i'm going to go from um first apa i'm going to import a first api so now let's create an instance of it so i'm going to instance of the first api so i'm going to now type in first api and then make sure i instantiate so i'm going to go ahead and actually save this and then bring back my terminal so in my terminal now i can just go ahead and actually run the code uh to act to to run open our development server so now this is all we need to implement a simple first api uh first api application first api api basically okay there are a lot of apis there but yeah that's just all this is all the code you need just two lines of code okay let me just go to the ui in the browser in the terminal and then start my project okay so i'm going to go uv cone and we will install uv gun that's how we install it so we can run our application so in the uv cone now the the place where we wrote this code is in our main dot pi file so we want to locate it so understanding and then colon and then up so the reason why we are saying app is the name of this instance of the first api library okay this instance that's facing up so if you name this differently then that's the name that you'll be using here okay in our case we called it up so i'm just going to call my main colon app okay run that so this is going to set our development server on localhost at port 8000 okay so i'm just going to open that up and you can see we have localhost that put 8000 and then you can go to documentation and you can simply type in your docs and then just return to you the documentation of the open api documentation now you can see this is just a basic uh api we don't have any route so whatsoever so now let's go ahead and actually begin to add different uh functionality to our aps and we have destroyed the development server and then i'll go back to my um to my application right here so now that we have that let's go and actually create a create a connection to our database so in our configs that uh in our configs folder i'm going to create a folder here sorry python file called data database dot pi okay so run that and then instead of our databases price we're going to install uh we're going to import from pi among pi we're going to install import the uh client great and then now we need to now go back to our uh browser and then get that first api get this copy this right here so remember how to get this you just have to go to connect and then click on uh connect my application make sure that you have the languages and the versions of python setup copy that and then go back to your uh visual studio code and make sure you paste that right there so now this is going to con this is going to create a connection to our database so let me just make sure i remove that since you already imported that that class right here okay so now we see this part here we have to remember we have to replace it right so if i bring you back to the browser it tells you that you have to replace this and also this so you can leave this by default by the password you have to change it or else you want to be able to connect your database okay so i'm just going to explain to you the uh to you this and i'm going to change this later on since i don't want you guys having access to my password that's why i'm going to keep it a secret okay and the best thing to do is actually to keep this in a of uh in a in a virtual environment but in this case i'll just keep things simple so let's say that the possible secret so you just simply type in your secret okay and i told you how to go the how to get the password initially okay great so now that you have that i'm going to simply leave this as a default okay or you can change this if you want whatever you want it to be okay so now we're going to create a database what i want to do is i want to call my database to do underscore app okay that's the database is i'm going to do application uh application so in there now we have our database mongodb is made up of a database and a database has a set of files called collection so we now need to create a collection and i'm going to call this collection i can think of decorations as a folder in each in each of this folder you have something called files like it something like that so i'm not going to go into depth about what mongodb is uh this is not a crash course on mongodb whatsoever so i'm assuming that you have the basic basic knowledge of what mongodb is and it's a non uh nosql database okay meaning that the data can be stored in different structures in form of forms called uh forms called the collections okay so now let's go and actually create the collection that you want so i'm going to go say a collection i'm just going to call this collection underscore name you can call it whatever you want and i'm going to say db uh sorry db which is uh this variable app here i'm going to create a database so i'm going to create a collection i'm going to call it to do uh to do is underscore app okay that's how we create our collection this is going to create a create a connection it's great it's going to create a database called uh the database called to do application instead of that database you're going to have uh to do a collection that to those underscore app collection so great so now that we have that there that's all we need so make sure that you input in your your password okay so i'm going to now uh input my password and then i'll be back to yes so i'm back to to the to the recording i've just simply type in my password and my password is in here now i can't really show in the content of this since it already has my password okay so let's get started and go ahead and actually create our uh continuity so let's go ahead and actually create uh in inside of each of these folders that we we have you're simply going to create uh a file python file and they're going to call it to do's underscore model dot py okay and then for the routes you're going to create another python file that called to do's underscore routes dot py and then in schemas again you're going to do something very similar going to call it to do's underscore uh schema dot py okay now that we have that there great so now we'll go back what you simply need to do now we need to go into uh uh we need to go into today's underscore model.pipe this we're going to create our model now model is just going to be a pedentic model okay so from uh by identics so i'm going to say by identique i'm going to import the base uh model okay so from pedentic i'm going to impose import the base model so i'm going to have a class here and this class is going to be called do and then this class is going to have a base model it's going to inherit from the base model so great so now that we have the the anemia each today is going to have a name and the name is going to be of type string so if you are not familiar with type hints in python and all of these are new syntax in python i have a whole tutorial about typings in python i'm going to leave a link in the description i'm going to put a card up here you guys can watch that video on type hints about python so it's a it's actually a one la one video single video about 30 minutes long i really i went a bit into depth about typing so if you don't really understand understand typings uh it's a good idea to take a look at typing since first api implements type hints in a huge huge uh detail okay so having a good understanding of typings in first api is just gonna set you forward and make up your code uh gives you ability to read code much more easily okay so description is each to do is go into the description and it's gonna be of type string and then you're going to have completed and then uh completed and completed is going to be of type bull the boolean and then you're going to have a date and a date can be something str okay something like that great so now that's all our database is going to look like our identity model is going to have each to do something of a name it's going to have a description of what you want to do and whether it's completed or not and then you also have a date okay and the date is just a string okay you can never remove the date part if you want but uh yeah and just go and actually remove the date part and then let's have a name description and then the completed or not create so now that we have that let's go ahead and define our schemas and see how our this is going to mark to our schema so i'm going to go into my schemas folder and then go to to those under to do's underscore schema.pi and in here i'm going to simply write a function uh sorry for that i'm going to create a function here dev and this function is simply going to be to do underscore serializer so i'm going to have a to-do series this uh to-do series is going to take in a single to-do and it's going to return uh to us a dictionary so i'm going to pass in a dictionary and what this to do is going to do is going to return it's going to return to us what is going to return to as a dictionary right because you are in here specify the typing to be of type dictionary so any i'm going to have an id so i'm going to have an id and the id is going to be uh sdr going to convert to do to do are going to get the the underscore id part of our to-do the reason why we are saying underscore id is that mongodb by default uses a underscore id for the id field rather than just uh id okay so that's a that's something to keep in your mind okay so now that we have that almost i'm also going to say to do and i'm going to get the the name part of her to do and then i'm going to go to uh description it's going to be our description so this description is going to be also be uh to do and then you're going to get the description filled the description so that's that and then we also need uh completed so after that here person completed uh let me just go back to my db model for once model and this is actually completed okay let me just make sure that i have the spelling right so to do and then i'm going to call this uh completed complete again to access the completed field so great so now that we have that done now i'm going to write another schema and this schema is going to serialize a lot a lot of those like more than one one or more to those okay so it's going to only handle a single to do well this is going to handle a to dose or a to do okay so i hope that makes sense so we're going to pass in serializer here and you're going to pass in our to do and uh and in here going to be going to return again you're going to be returning a list okay so we're going to be doing a list rather than a dictionary okay so what you're saying we're going to do here writing a list comprehension and i'm going to say to do a serializer okay and for each of that you're going to pass in it to do and how you need to do that i'm going to say for to do in to do's okay so this must be to do is my bad so this is do so for each to do into to do so you're going to call the single to to do serializer to serialize it so if if a single to do is passing here then this will be closed and then we'll return a list at the end if there are many to do they also uh each uh for each to do uh a to-do serializer be called okay so that's all for that now that we have that are basically done with our schema these are just this you can think of a schema under like a serializer or how uh how we control how we present our data in in first api so he says if you're coming from django uh django rest framework then this is the same concept as a serializer over there okay so now that you have all that set up we are mostly you're basically done so what you're simply going to do now is to build our routes so now we need to build our routes and um we need to need to go to uh our routes and then go to to those underscore uh to do's underscore routes dot pi and in here that's we're going to implement our route so uh what we need to do first we need to import something so we need to uh sorry uh we need to import it here let me type in the right uh place so i'm going to say from first api i'm going to be importing um api router so great and i'm also going to be importing uh something to our model in our schemas right here so i'm going to going to say uh from uh let's first import our database uh our database so the from config.database we're going to be importing our our collection collection name because we're going to be using the collection to interact with a db okay okay so from uh models you're going to be a models dot to-do model you're going to be importing uh we're going to be importing a couple of uh uh something from here you're going to only import imported to do and to do is our simply our database right here this class right here so great and also uh for now we also need to import from the schema so from uh schema so from schemas that to do schema we want to import uh we want to import something what we want to import in this case i want to import to do's as to do schema this actually to do's uh to do to do underscore a schema and we also want to import to do's underscore schema okay so these are to do decided to do schema okay so there's a rule and singular right there so now that we have that setup let's go and actually create a basic uh api route uh object okay so we're going to say to do underscore api uh underscore router and this is going to be an instance of api router just like that so now that we have that let's go ahead and actually write the crowd functionality so the first one i'm going to write is a retrieve functionality okay we're going to write the retrieve and then the retrieve is the easiest right so what you're saying we're going to do use a decorator and i'm going to pass in to do uh to do underscore api ser api router so if you're not a decorator is in python i have the whole series on what the decorator is in python so you can check out the other video above there so basically what the decorator is that i can explain to you in a simple terms a decorator is a callable colorways because basically a function so a decorator is a color but that takes in the color bars and inputs and returns a callable as an output so if you have taken a function and before you execute that function if you want to do something else then you can use that uh decorator that's basically what the decorator is on the surface level so if you want more in there i have a whole series on decorators so you can check that out okay so you're going to pass in async and i'm going to have a davey i'm going to call it get underscore to do okay uh this is going to be good to do so that so in here what i'm going to do is i'm going to go go to juice it'll be equals to this would be to do's uh to do serializer so make sure i have to do serializer and then i'll make sure it's this is plural singular okay so collection name dot dot we want to just say fine just like that so this is going to return to us all the to do from our database and what is i'm going to do is continue to return it to that the user will make that request okay so you can actually pass it in here you can say um first conditional you can say status they're going to say status is of okay and then you can pass in here data and the data is going to be our or to do's right there so great so make sure i change this to be the right spelling so to do so this is going to get all the data from our database and i think we're going to return a dictionary having the value key it's going to be okay and then the data it's going to be the order to do okay so i'm going to go ahead and actually save that so now that i have that saved let me go ahead and actually show you something so uh i'm just going to go ahead and go to my terminal so just go to back to my terminal go back to my terminal and then here i'm going to run i get run again my pa my app so just uvicon space main and then colon app okay so now that i have that running i'm going to go back to my browser and then go to our swagger ui here and then i'm going to refresh this and then um it doesn't actually show up okay now i know why it doesn't show up my bad i forgot one thing we need to do okay we created the the main. uh ips so we didn't actually import our routes here so let's make sure that we have that routes uh imported right there so what you simply need to do is go ahead and actually import that route and then that program should be fixed so we need to import the routes that we created from our uh to do zero we need to import this uh this uh ap to do underscore ap underscore router we need to import that shelter into main system the entry point digital application is in main right so great so now we need to import it from here so i need to say from to do from from um from router so from routers dot to do router uh routers or routes are going to import uh to do uh api so we're going to import to drought to do underscore api and that's called router grid so now that we have that there i'm simply going to go and you're saying app dot which is our instance of the first api and then we're going to include router and then i'm going to pass in our to do uh api router right there so great so now that we have that i'm going to go back to my terminal and make sure that i stop my development services i'm not using the reload flag okay so i'm going to start that again so we have an error here so it says that people install uh pi it's like it it can't actually find this but you already installed it so let me go and actually install this again so that the what it said was let me just scroll up here it said you installed this so i'm just going to go ahead and actually copy this and make sure that i install it i don't know why we're getting this error because you already installed it prior but uh let me just paste that right there again if you also get the same error then i'll try to do this okay yeah guys so you can see now that's probably installed so i'm going to go and actually start my application again so now that's running great i'm going to go to my browser and then i'm simply going to hit refresh now you can see we have a router called get whenever i go on try and then execute this this is going to take some time and then it's going to return to other responses that has results the status quo is 200 and the status is okay and then you can see we actually have no to-do's for now so great now that works great so let me just go back here and then i'll stop my development server click this terminal then let's go back to our our visual studio code so now that we are here let's look at other functionalities uh to get uh we can this is this is going to get all the to-do's now let's write the code to get a single tutor okay so so now let's write the code to get a single single to do because this is going to be done to us all day to days okay so for now we don't have any to do so i won't be able to demonstrate this to you but uh just bear with me so this is also gonna be a get request and uh just make sure i have that there so uh in here what we need to do we need to get first get an id right we know which to do to get now uh this is how we get an eye created dynamic url in first api again if you're very familiar to first api i have two whole series on first api building an e-commerce api using fast api library and also building a full stack react in a fast api application so you can go and check that out so this is how basically we create a dynamic url so the user has to pass in the id of the of the of the to do that he wants and then we'll be using we'll use that id to filter our database okay so great so now we're going to have an async function here so ac is going to be def this is going to be get underscore to do okay this the previous one is get to those and this one is going to be get to do you know person is uh um you need to pass in a parameter uh an argument here a parameter rather called an id and it's going to be of type str and also we need to uh yeah that's what we need okay so now we that we have the id what you're saying going to do is you're going to return you're going to return to do's right and then we're going to say collection collection dot uh dot this is gonna be defined and i'm going to pass in a dictionary of id and the id is gonna be of of type of now we need to import another library here so the reason why we are doing this is collections.name uh collection collection name of dot find now we're going to pass in the id i'm going to pass in the idt's next okay just in a second uh so that the the cursor object will be passed into the serializer uh function which is going to move to this schema and then make sure it's going to come over here and it's going to loop through get the then call this uh serializer and then it's going to serialize the data into this format that you're going to return to the front end okay so that's basically how it's done so for us to be able to specify the the id you need to import something from first air from uh python so you need to say from uh this is gonna be bsum you're going to be importing uh object object object id right there so now what you need to do here we need to say object id and then you're going to pass in the id that has been passed in so that's all we need to get that from our database okay so i just need to say id and the id is going to be the one that you have specified okay so this is gonna be underscore id so uh we're going to look through our way to filter our collection and find the the the document with the i provided id that has been passed in and then we're going to now serialize it and then simply return it to the user i need to return it you can also make sure that you can use this so this to be consistent we're going to call it to do and then i'm going to assign it to this yeah and i'm going to assign that to do to this value so i'm going to we're going to return and i'm simply going to copy this right here and then paste it here and then change this to b to do that's all we need to do so great so yeah that's all we need to do so we'll get the data filter based on id and then serialize it and then simply return it to the front-end grid so now that we have that and uh let's go ahead and actually write up one method to post a database so let's write this so we're going to say the same thing again to do api uh underscore api router this is gonna be a post request okay and for a post request we now we need to get um we need to get the data from the front end right so what this i'm going to do now create a new sync function here and it's gonna be post underscore to do okay and then you're going to now get the data from the front end and the way to get that from the front end simply saying to do and it's going to be of type uh to do okay again if you have no final typings like i recommend you to watch that series on typing that i have on my youtube channel okay so now that we have that there let's go ahead and actually now get the information so we're going to simply go put this into a database so to do this you're going to save collection collection name collection underscore name uh dot this is gonna be insert underscore one so you can see i haven't having i'm even getting an auto complete uh there so you can see we're going to insert one and you're also going to decide we're going to convert that to do into a dictionary okay make sure that you convert into this into a dictionary or else you have an error so again we say collection name dot insert one and what i will do is search in the dictionary of what the to do that you have got from the front end so this is going to return to us an id now we need this id in order to get the data that has been just inserted and returned back to the front end right so i'm going to now pass in your underscore id and this is going to be uh that's what the idea that is going to be written so this is going to grab the the to-do then insert into our database and mongodb is going to return to us the id that was used for this uh insert operation and then based on that uh id you're going to now get that to do from our database okay so then we're going to copy this and then uh paste it here this is this is basically the same thing so paste it there and then this is going to be uh underscore id so that's basically what you need to do [Music] so this is going to be uh rather sorry i'm about so this is going to be uh this is going to be underscore id dot insert id okay that's what we need to do so the id is going to be uh underscore id dot insert id okay not the way i said it previously now that we have that so we're going to return that to do to the front end so great i'm going to now paste that there so this is going to insert the data into our database it's going to return to us the id that was used for that insert operation we're going to be using that id okay using that id to filter our database and then simply return that the post that was the to do that was just simply posted uh posted so now that's great now go ahead actually save this now let's go back and try our application to make sure everything is working until we have reached so uh sorry i'm going to stop this and then clear the terminal and then i'll start my you app again so now everything is running so i'm going to go back to my front end here and then try to refresh this so you can now see we have one for post and then one for gateway text in an id okay so let's try down for post and then you can see this what we need to do so i'm going to click on the try here and then i'm going to simply say um i'm going to say learn db and then let's say the description is uh learn by mongodb okay just like that and you can see that this is full so now since you guys are still watching uh the so we're going to say that to be false and then we can simply go ahead and actually execute that so this is going to send this back to our uh mongodb database and you can see right here it has been inserted and we have all these fields written back to us with all the the data that you just provided so if you go back in here to your your at last dashboard and you try to go to uh database room just click on this part here and uh let me just go sorry monitor let me just go back go to collections and in collections i'm going to go to let's just wait for this to load so it's taking a bit of time uh but you can trust me that that has been inserted so i'll come back here whenever this is loading so right now it's very sluggish so let's see why it's so sluggish okay we got that four four errors so let's just go back here yeah but guys that's basically it so you can see here the data has been inserted and the id has been written so let me just copy this id and then let me minimize this and then go to uh the part where we can so let me go back to the part where i can just filter it to do basing on the id so i'm just going to try this out then i'm going to pass in the id that i just got and then let me just execute this so you can see now we have back that to do return to us by providing that id so that's how it works so now that we have that done let's write the uh the functionality to update our to do so i'm going to go back to my terminal and make sure that i kill this process for now kill the server and then go back to visual studio code now let's write uh the other uh functionality to update update to do okay so in in-house we're going to do in here is uh we're going to go ahead and actually write the functionality and it's very similar to the ones that you have done so far so going to be to do underscore api router and then this is going to be a put request okay first you're going to update something and i said we're going to update something with a given id so we're going to write a dynamic url again i'm going to pass in that id then you're going to have nsync function and it's going to be def it's going to be called update update underscore uh to do and this update to do is going to simply go into taking a to-do or from the front end so we're going to pass in two things right a knight is going to be of type string right it's like we did for this patch here and also now we're going to expect it to do right so you're going to be of type to do and in here so we're going to do is uh we're going to now get collection dot collection underscore name right so collection underscore name dot uh find and update okay so find and update find one and update okay because you want to update only one thing okay so we're going to pass in here uh we're going to pass in underscore id and this underscore id is going to be now this way we need to use the object uh objects object library that we just imported or it just imported so object id is going to pass in our id that has been passed to the front end and then also you now need to go outside of this dictionary and then pass in another dictionary okay and in this dictionary i'm going to do we're going to pass in a variable here called uh set okay and then we're going to pass in this i'm going to say convert this into a dictionary then i'm going to pass into two that's all we need to do to update the given to do so this is going to filter it based on the provided id and then it's going to use the data the data that has been provided from the front end to actually uh uh updates that value okay so it's going to convert the data from the front end to a dictionary and then update that value right there so now that's done you're going to get now return or to do to the front and so uh now we're going to go ahead and actually get to do another to do that you've just updated now you might be tempted that you're just going to put it to do here and then do something like this now if you do this what's going to return to it's going to return to the to do before updating it okay now you wanted to do after update after it has been updated okay doesn't need to do this afterwards so i'm going to do collection uh creation underscore name dot uh find so i'm going to find and we're going to pass in unless and uh underscore id and our id is going to be id is going to be or the object again so we're going to use the object function object id function and simply pass in our id and that's all we need to do so this is going to now update it based on the using a filter is basing on this id and then update it and then you're going to filter it uh to get the updated one updated version and it's gonna be simply going to return the front end so i'm going to copy this return statement here and then simply uh paste it here so that's all for the to for the update functionality now let's go and actually write the one for the delete and then we'll be drop down for this video so i'm just going to uh do the same thing here so this is going to be the same thing as we have done so far okay so i'm just going to go ahead i can even copy this and modify it if i want so i'm just going to copy this and then paste it here uh sorry just bring back that so i'm going to go ahead and ahead it's actually it's going to be changes to delete so delete delete to do and then this is everything is going to be as it is and then to delete something we don't actually need data from the front end so i'm going to delete that part and then also delete this part right here okay so delete all of this so great so this is just going to find in this zero find it update is going to be find and find underscore and delete okay so find and delete with this specific id so that's all we need to do and after we delete it you're simply going to return to the user nothing writes an empty list here so it's going to tell the user status okay and getting that indicating that everything has been deleted and the data is simply going to be an empty string so that's all for uh the crowd functionality okay so let's go ahead and actually try the update and the delete functionality okay so i'm just going to go into my front my terminal and then make sure that i have my uv constructed and now it has started so i'm going to go ahead and actually refresh this page and then minimize this so you can see we have the to do here so i think i made an error so let me just go back to my terminal stop the stop this for now and then go back to my vs code so this is going to be uh this is going to be a deleter of not a put shot so it's going to be delete okay that's one of the issues of copy pasting code so make sure that you change that to delete so so i'm going to clear this and then start my application again and then go back in and then try to refresh hopefully we have that delete route yeah and you have it here so i'm going to go ahead and get um try try this out and then execute this is going to be done towards a single to do so you can see we have it right here so i'm going to copy this id and i'm going to use it in my other places okay so in our other route so i'm going to copy this and then minimize this so i'm going to go into my update so i'm going to go into update and i'm going to click on try pass in the id there and then in here i'm going to go ahead and actually grab this from here so i'm going to grab this copy that and then uh bring it here and then simply paste it here so you don't have to type over that all over again okay make sure i have that there and then i'm simply going to make sure i fix that formatting like that and also this part and this part okay so now what one thing i notice is you don't need this id whenever you're trying to update it so we're going to delete that id so i'm going to learn mongodb and i'm going to change this to be completed to be true see obtaining we have tinder completed to be true we can go ahead and actually execute this so press on the execute button and this is going to we got an error so we got a 500 error uh this is an internal service error so let's check what the error is you say object object is not iterable so it's saying that um so let me see where this error is coming from it says that is not it treble so let's see where the error is coming from so let's try to trace it okay so it doesn't really have a good error output so let me just go back to the code and then let me just check where i probably went wrong okay so we have um we have it right here so okay the reason why i got this error is because i need to now uh i need to now serialize this to do before saying it to the front end so i need i'm going to say uh to do to do is underscore serial serial serializer it's realizer and then i'm going to now rub this in here okay so my bad i forgot to serialize that before passing it to the front end okay so now that's done yeah that's basically it so i just got the data from the database we make sure we serialize it and then pass it to this dictionary okay great so i hope that era is fixed so let me just go back to my terminal and then clear stoplet server clear the terminal and then start my development server again then go back to the here so i'm just going to go and actually execute this one more time now you can see that we got back it has been now changed everything has been updated okay so now let's even try to update this learn pymongodb today with code with principle just so then let me just now go ahead actually execute that so this is gonna now we can see now our data has been updated so even if i move up here and then try to execute this we can see now it has also been updated right here so great so now that we have that let me just go ahead actually copy this id again and in the delete section i can simply go and actually go ahead and actually click on try and here i can paste that specific id that i got and i'm going to execute this this is going to go and actually delete it delete it and you can you can see it returns to us a status of okay and the data field being empty okay so if i even if i go up here and then try to get uh it to do's and then you can see now we have no to-do's in our database so let's go and actually insert one more to do that so go post and then i'm going to go ahead and try this out and then let's say um let's say uh learn let's say mongodb let's just keep the same thing we had so i'm just k here uh learn uh db with code with code with prince okay so now let me just have that done and i'm going to simply pretend this to be false it's not completed and i'm going to go and actually submit that and you can see we have back uh 200 okay and everything has been uh is as as we expect okay so even if i come in here and then try to execute this now we should have one to do right there okay and let me just come back here and i go into clusters and then i can see it's going to show me the classes the collections that i have rather and cluster so this is a bit slow i don't know why their website is a bit slow today so you can see now we have our database right there and then our database is called to do application and that's the normal database that we we gave it and we have the cluster here called the dues app and you can see here we have the only one to do in there so even if i add a new to do in here so i'm just going to go in here and then go to post and then uh post here we can go ahead actually say this the second let's add that there and then make sure execute this and this can see we got that box even if i come in here and then try to refresh this and then i'm going to get now the newly updated value so i'm just have this refresh that page and this is going to return to me uh the new to do that you have just added okay so yeah that's basically it for that uh how to create a simple to-do list uh to do application using uh fast api in the mongodb library so you can see a second to do is this one right here and that's how we can be able to implement a to-do list application using uh first api and uh mongodb so guys thanks for watching if you enjoyed this draw so far give this video a thumbs up if you want to learn more about fast ap i have a whole i have a whole two series uh two series on fast api first api with react and the first api you to to build an e-commerce back-end okay so if you guys are interested in that tutorial we have authentication he's sending emails uploading files profile pictures and all those fancy stuff you can just check out that tutorial i'm going to leave a link up here so that you guys can find uh that tutorial so guys thanks for watching uh see you in the next one keep safe uh make sure that you subscribe to my channel if you haven't it really encourages me to keep making this video thanks for watching see you the next one
Info
Channel: Code With Prince
Views: 16,535
Rating: undefined out of 5
Keywords: fastapi, swagger, rest api, api, crud api, python mongodb, python rest api, python fastapi, mongodb, pymongo, FastAPI, MongoDB, react JS, farm stack, full stack application, tech with tim, pymongo tutorial, pymongo tutorial python, python mongodb tutorial, mongodb python tutorial, mongodb python, pymongo python, how to use pymongo, how to use mongodb, mongodb tutorial, nosql, document database, mongo, mondgo db tutorial, mongodb course, mongodb tutorial for beginners
Id: MXwcUrI-iss
Channel Id: undefined
Length: 48min 35sec (2915 seconds)
Published: Fri Aug 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.