PyMongo (Interact with MongoDB in Python) [2022]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello coders welcome to my channel coding101 with steve in this video we will be learning pymongo we are going to learn what is pimongo and why do we use it and then we will be learning how to use pymongo in your python application so let's get started pymongo is a python package this package contains tools or functions for interacting with mongodb database from python so you can perform different operations on mongodb using the functions of this package such as the crud operations that is creating the database collections documents and also reading updating inserting or deleting the data using this package you can connect your python application to a mongodb database so if i have a mongodb database and i want to access it from a python application then i will have to use pymongo so now we will be learning how to use pymongo in python as we learned before that pymongo is a python package which we use to interact with mongodb database so that means we will require a mongodb database or else what are we going to connect to right so let me just tell you quickly there are two ways in which you can have a mongodb database one is that you install mongodb into your system follow all the steps and set the username password host port number everything and you know you make the setup ready and then you can connect to it from the python application and the second way is that you can create a free hosted mongodb database using mongodb atlas mongodb atlas is a global cloud database service that is fully managed by the team behind mongodb so it's a fully managed cloud database where you just need to login and within few clicks your database is ready let's go ahead and learn to use my the very first step is to install and import the pi python package why do we need to install it there is some code in that package which we need in order to interact with the mongodb database and the problem is we don't have that code into our system that is why we have to install the package and then include it into our code so now you need to pay attention and listen to me carefully to install the pi package we will use the command pip install by pip install by enter successfully install pymongo 4.0 now once we have installed it we need to import or we need to include it into our code import by that's how you include the package inside your project or application and you save it and by this we have completed our first step that is to install and import the pi package as we finish the first step now we are going ahead on to the second step the second step is that you need to create a mongodb client as i told you before that this package pymongo contains tools or functions that we will be using to interact with mongodb database so there is a function or there is a class inside this package called client we will be creating the instance of this class as i said we will create a client and to use the function or the class of this package we'll do by dot client you see that i've just created an instance of this class i've created a mongodb client so in this function we will have to pass a connection string okay let me just write it connection string okay creating a variable and inside this i will put my connection string and this connection string i will have to pass it to this function and then the connection will be established okay if i call this without passing any connection string then this will connect to the default host and the port which is your localhost and the port is 27017 that's the default port number or else also you can you can pass the host in in this first parameter and in second parameter you can just pass the port number and you know you can get connected to that host and port number but now we have a connection string already okay and i will be passing it i will show you from where i'm going to get this connection string as i told you that we will be using mongodb atlas so i'll just show you how to get the connection string from mongodb atlas okay i already have created a database inside this we have to click on connect and then choose your application you need to connect your application right we want to connect it through my application so from here they will be giving me a connection string there you go you can you know you can select it for different languages you see cc plus plus java node.js but yeah we have python over here python and the version i prefer 3.4 or later now we need to do two things just read this they said replace the password with the password for the username and then replace my first database with the name of the database okay so i have to replace this with my username this with my password and this with the database that i'm going to use and after copying it just come here and i will be pasting the connection string here okay so my database i will change it where is it okay my first database i am going to change it to my apps and my username and password is oh i'm not supposed to share that right so i just type it in okay i have entered my username and password i have entered the database name and i think we are good to go now i will be passing this connection string to this client and just to make sure that nothing goes wrong we will just put a try catch in this or a try accept okay so accept exception and this and now i am going to save this i've just put a dry catch or a try except it will try to run this and then if it you know if it gets any exception if it gets any error it will catch it in this it will come in this exception part and it will print for me what the error was that's how i'm going to run this okay to run python and what's the file name file name is pymongo there you go enter if we are not getting anything that means we are able to connect it successfully let's see what happens bingo yes we are able to connect to our database successfully congratulations we just learned how to connect to a database using pymongo okay coders so now that we have established a connection to the mongodb database we will see some crud operations so now we will learn how to create a database the step three will be create a database or a tv now we will use this client to create our database so i can say mydb i'm just giving a name you can give it any name i'll use this client and give here the name of my database it can be anything let's say by demo that's it by this line we have created the database into which we will store our document and i'll save it okay you can also list out the databases that are there inside your mongodb so to do that i'll just print and show you we will use the client again and list database names there you go i'll just print this okay when we run so what we'll get is the databases that are already present so i'll save this and i'll come here and i'll run okay so i got there is a database called my app underscore db there is admin that is local these three databases are there you might wonder we just created a database name called pymongo underscore demo but why is it not present in the list yes don't worry that's not an error it works that way i'll tell you the database that you created will never show up unless and until there is a document inside that okay so the database should have a record inside it in order to list it only then you can see that the database is created and we will see that so for now let me just comment this and we finish creating our database now step four create a collection if you don't know what collection is uh if you are familiar with the mysql if you are familiar with the relational database then collection is equivalent to tables you know where the data is stored into rows and columns okay so that's similar to collections so we will create a table inside our database so to do that this time we will use the database as the reference okay and using this reference we will create our collection so i'll write my collection and then i'll use idb the way we created a database same way we have to create the collection whatever name you can give i'll give demo collection okay here now after this when we execute this our collection also will be created but still our database won't be showing up because as i said it requires a document inside it or a record inside it if you want we can just check it again i'll save this i'll go here run the command enter you see still the same result okay so i'll come in this now the next step is step 5 create a document create a document now the document or record that we store into a mongodb database is in json format so i hope you all know what is a json format so we will create a document in json format this will be my doc and i'll show you this is how json format is i'll write a key that is basically key value pairs okay so the key is name and the value against this key is my name steve and maybe i'll give another key saying message and give the value as this is by demo bingo we have created our document so coders by this we finished the c part of crud operation we created a database we created a collection and we also created a document but still the database is not going to show up for that we will have to insert this document into our database into the collection which is into this database the next step is insert the document okay here we are going to use another tool or a function of this by package how to insert a document just think about this where you have to insert the document inside the collection right so which reference should we use to insert the document the collection object right so we'll do that we'll say my collection and as i put a period we get a method called insert one insert one insert one basically means that you want to insert only one record and if you want to insert multiple records okay then we use insert underscore many since we have only one record we are just going to use insert one and we need to pass the document to this method so i'll pass the document that is my doc the insert one method will return a result that result will have an inserted id that is a specific id that you give to each record mongodb by default gives a specific id to a record it's going to return a result in which one property would be inserted id by that you will also come to know that the record has been successfully inserted we do this so whatever this will return it will come in this and in this we can get the you know the inserted id let me just see if we can do that see okay i'll show you that also print okay we'll just print the inserted id and also we'll print the database names so then you will come to know that that the database has been successfully created because the record has been inserted okay this is just a unique id if you don't want the by default id then you can just you know you can just do like this id key maybe say one yeah so by this the inserted id will be 1 itself and if you don't put this if you don't put this then the inserted id will be some unique id which mongodb will give it by default so as for now we'll just execute it like that i'll save this so now we will run the file and let's check what what we get we are expecting some id into this some unique value over here and we are expecting that we will see pi underscore demo in this list okay so let me run this and there you go this is the unique id that mongodb has by default given and this is the database that is created and this is the record that is inserted into the database if you're learning something from this video then please do not forget to subscribe to my channel in order to be notified about my latest videos we finished creating a database creating a collection creating a document we saw how to insert a document into the database and now we are going to read the document that we have inserted so the next step step number seven reading the document okay so now how to read the document see the document is always present inside the collection so we always have to use the collection object you know some because the starter used to get little confused between which which one to use should i use client should i use my db should i use collection what to use but yeah when you understand logically then you will not be confused so we'll use my collection we'll just give with the name as record we'll use again as my collection as the object and you'll see there is something called as find underscore one this means that find one is going to return to you the very first document the latest document which is updated inside the database whichever is on the top you will get that now when we print this before executing this program we need to be careful because everything is going to execute again so if everything executes again that means it will again create a document correct and it will again insert that means there would be replication and then that's a problem if this code executes again then you notice that it is going to create a document again it is going to insert it again you see so we'll have to comment it okay we don't need this now and by the way don't worry it is this is not going to create the collection again if there is a collection already present then it will just refer to that now i think we should save this we will run the file yes so we got the data you see the record we got it we got this id you see the name this is the message this is by demo so that's the record that we had inserted inside the database we were successfully able to read the data if you have learnt and understood till here that i would like to upload and give you a thumbs up let's move to step number eight so the step 8 is updating the record that we have inserted now what is the so you might think what is the difference between inserting a record and updating a record let me tell you in update we do not insert a new record we just update the existing record whereas in insert we insert a new record into the database so that's the basic difference i'll just show you new record equal to my collection this time we'll use update one now this function or this method takes two parameters first is a query and the second one is new value the first parameter is the query object that defines which document to update and the second parameter is the object that defines the new value for the document so i'll create a query object a dictionary and now we need to show which record we want to update and the record is message what is the message that we are given this is primongo demo i'll copy that i'll paste it over here okay so my query is that record which has a message key with a value this is my mongodemo and now the other parameter is new value again i'll create a dictionary and over here i'll show you this dollar set this is a built-in key which we use to set a new value to the record and this also has a dictionary the key would be same the key has to be same and now i will change the value to instead of this is by demo i will put welcome to coding 101 with steel so this is my new message that i want to set okay so i've defined the two parameters and i'm passing it to this function update one and we'll just read the document again after we update it so i'll copy this and paste it over here this is reading the document after sorry after [Music] updating reading the document after updating okay i'm reading it and then again printing it so now i will save this and let's hope we get good results so let's see python [Music] whoa what just happened this was my previous record which had message this is by demo after that we printed the new record again and now this time the message is changed welcome to coding 101 with steve wow we just updated this message congratulations that's how you update a record into your database good news is that we have reached the last step of this lesson and that step i think it's number nine is to delete the record that's the final step now we want to delete the record now again we'll use my collection then this time we have another method called delete1 and this method takes one parameter that is your query in order to know which record to delete i'll just name it as query underscore okay now we need to delete where key is the name and value is steve that's the record that we want to delete query underscore delete i'll paste this here again we'll have to read the document after deleting it so i'll paste it here but yes before executing we'll have to comment all this because we don't want to update again right so from here till here restart the database names and then the record after that delete the record where the name is steve and then again print the record i think this is good we'll save it and execute whoa see we got none so this was the record before we deleted it and after deleting we got none that means we have successfully deleted the record so coders we just completed all the crud operations that is creating reading updating deleting including inserting in my upcoming videos we'll see how to create a database on mongodb atlas and we'll also explore the other methods or functions in detail and maybe create a desktop application so i'll see you in my next video thank you so much for watching
Info
Channel: Coding 101 with Steve
Views: 17,453
Rating: undefined out of 5
Keywords: pymongo, pymongo 4.0, pymongo 2022, pymongo python, pymongo 4, pymongo mongodb, pymongo tutorial python, interact with mongodb in python, pymongo [interact with mongodb in python], pymongo interact with mongodb in python, pymongo interact with mongodb in python 2022, interacting with mongodb in python 2022, mongodb python 2022, pymongo in python 2022, mongodb in python 2022, mongodb python, python, mongodb, pymongo crud operations, pymongo mongodb python, pymongo in 2022
Id: GJCKIGeK3qc
Channel Id: undefined
Length: 26min 7sec (1567 seconds)
Published: Tue Dec 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.