MongoDB Crash Course With Python 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
mongodb is one of the most popular and also one of the most fun and easiest to learn databases out there so in today's video i'm gonna give you a crash course on mongodb and how you can get started with it we start with a little introduction about mongodb and then i show you two ways how you can get started and connect with the database either locally or in the cloud through atlas and then i show you how to access the database in your python code and then we go over the most important functions so let's get started and by the way if you're new to the channel i'm patrick and i do weekly videos on python and machine learning and if you enjoy my content then i would be very happy if you leave me a like and subscribe to the channel mongodb is a document document-oriented database this is also classified as a nosql or not only sql database so here we store our data in collections of documents so let me show you how this looks like so this very much looks like a json file so if you're familiar with the json format then mongodb should be really easy for you to pick up so in this example we want to model the database for a to-do application and each to-do item we store in a document so the document in this example looks like this we have an id this is a unique identifier for this item then we have a name and a text and a status then we have text for this to do and a date and what is special here for example is that these tags here this is a nested document so here we have an array and then again inside this array we have different dictionaries with a tag name and a category so this makes it really flexible so we can just put in this nested document inside the whole document and we also don't have to think about this beforehand like with relational databases so there we have to think about how we structure our table and the different columns and even the data type so we don't have to do this here and this is still really fast and performant so the data is actually not stored in the json format so the format here is called bson which is binary json and this is highly optimized and really fast and what is really cool here is that we can also query for this nested types for example we can query for the tags with the tag name python and then we still get all the to do items back with this nested item and this is really fast so we will see this later in action and now in this example so we model these to do items and we use a slightly more simple version so for the text we just use a nested list here with a different text so this is what we are going to model in a second and now let me actually show you how we connect to a mongodb database so now let me show you how to connect to your database and for this i want to show you two different options either locally or in the cloud so let's start with the local version and for this you want to select software and then community server on the official homepage and here you can download this and i think for windows you actually have to do this so select your platform and download this and install this you could do the same for mac but there is another recommended way and you find this in the official installation guide so i can put the link in the description there you find installation guides for linux mac and windows so for mac the recommended way is to use homebrew so you need homebrew installed and then you just need these two commands so you say brew tap mongodb slash brew and then you say brew install mongodb slash dash community and then you can specify the version so i already did this and now you can open your terminal and now you can run this as a background service so you could say brew services start mongodb dash community and the version and hit enter and then in my case it says it's already started so you could do the same by saying brew services stop and this would stop the service again so let's start it again and now it's successfully started this in the background and now we could type in and this will give us the shell and here we can interact with the database so we could actually just work from here but i also want to show you how to use a graphical user interface for this so let's hit ctrl c and get out of this shell here but we still have this running in the background and now what you want to do is you want to install the it's called compass or just compass so you find this on the homepage under software and then compass and there you can install this for free and then install on your machine so i already did this so let's open up mongodb compass and here we have our graphical user interface so we have running as a background service and now we can click on connect and then by default we already have three databases admin config and local and we can create our own one so you want to click on create database and then give it a name let's call this test database and we also have to give it a collection name so let's call our collection to do's and then click on create and then we already have this here so this is our test database and we can click on this and then we see the different collections so here for example we can click on create collection and create a new one um and then if we click on a collection then we see the actual data that we have inside this collection so here we would see our different to-do items and we could import data here but in this video i want to show you how we add the data through our python code so now in order to connect um in your python code to this database what you want to do is click on connect and then disconnect again and then here you see this connection string so this one so you want to copy this and paste this into your python file and i will do the same in a moment but now i also want to show you how to use the cloud database so we set up a cloud database and then again we get this connection string and then we can continue from there so now let me show you how to get started with a cloud database and for this we use the cloud atlas product and you can get started for free so you will get a 512 megabyte storage for free so this is really cool so let's click on cloud and atlas and then of course you need an account so you can sign up or here i already have an account so i can just sign in and now here after signing in you want to create a new project so let's give this a project name let's call this tutorial and then click on next then here we could add members and set permissions but we can just click on create project and now we have one project and now we want to build a cluster so click on build cluster and then here we see that we get a free shared cluster so we want this and now here you want to select a cloud provider and the region so usually you want to select the region that is closest to you so in my case i use frankfurt in germany and then we see we see with this free version we get 512 megabytes storage and yeah we are good to go with this so we can click on create and we get this for free then and now this will take a few moments to set up so now our cluster was created and then we can click on connect and here we have to follow a few steps so we have to add our current ip address so let's just click on here and add this one and then we have to create a database user so for this let's just use the user uh let's call this patrick and i also give this a password so you can generate one but in this example i just use one two three four and then click on create database user and now we click on choose a connection method and here we use connect your application because we use our python code and then so click on this then select your driver so python and the version and this is not the python version but the version of the driver so we use this the latest one and then again we click we get a connection string so this is similar to the first one that we get when we set up the local database so we want to copy this and remember this so let's copy this and close this and now let's actually open a editor and open a new python file so here let's call this main.pi and then we want to create our cluster so let's say cluster equals and then this is a string and now we paste in this connection string so here we already see we get our user patrick and then we have to use the password so in this case this was one two three four so obviously i'm gonna delete this again after this video so you can't mess around with this and also obviously you don't want to use a easy password like this and you also in your production code you don't put this in your python file so you want to store this credentials separately and then load them from a file that you don't deploy so now we already have this string in our python code and now let's go back to this site so before we connect this in python we click on collections and then we also create a collection like we did with our local server version so here we click on add my own data and then we click we create a database so let's give it a database named test and a collection name so let's use to do's like before and then click on create all right so now we have our database and our collection and here we can add the data so now what we want to do is in our python code so we need a driver and a driver is basically the one that talks to the database so for there are two official drivers that are recommended so either pymongo which is the normal one or if you want an async driver then you want to use motor so in this case we just use pi so we have to install this so we say pip install and then buy and if you also use a cloud version then you also have to install dns python otherwise you will get an error so go ahead and install this and hit enter so in my case this is already installed so if you use a local database then you don't need this dns python and you could recognize this because then your connection string don't has this plus srv here but in my case in our cloud database we have this so now we can also modify one more thing so in our connection string instead of just my first database we can use the test na the name test of the database that we just created so now we have this and now we need to import our client so we say from pi we import the client and then we set up a client so we say client equals and then our client with the connection string that we put inside this cluster variable so now if we run this then we should already be connected so for example now we can print clients dots and then we can say list database names and now in this case we can access a database by saying database or let's say db equals client dot test so you could also use this like a dictionary so you can use it like so client test but it's a little bit easier to just say client.test like this so now we are connected to this database and now for example we can say prints database and here we can list the collection names like so so let's run this and see if this works so we get a authentication error so i'm not sure why i thought the password was one two three four and also the user so let's go back to our cloud database and let's go back to database access and edit our user patrick so let's click on edit and by the way here we could change this that we are not an admin but just read and write database this should be enough and let's click on edit password and let's actually click on auto generate and show this and copy this and update user and now let's go back and paste this in here so this is the place for our password and now we have to wait until until this is deployed all right so this was deployed so let's run this again and now it worked so i'm not sure why the first password didn't work so either i made a mistake or it didn't accept such an easy password so but now it works so now here we see we get our list database name list database name so we have test admin and local and we have the collection names so here in our test database we have the collection to do's so now we could set this up and now let's actually add data into this so now to add data into this we want to create a example data so in this example we want to add a to-do item so let's create this and now here this should be a dictionary so we create a dictionary with the different attributes so let me actually copy and paste this in here so our dictionary should have a name and then the name of this user patrick then it should get a text my first to do sorry then it gets should get a status so the status is open or closed then we have tags and now here this is a nested document so here we use an a list so it gets the text python and coding and we also want to have a date so we could actually just use this as a date time object like this and for this we need to import date time so we say import date time and then we use datetime.datetime utc now now let's put this in our database and we want to put this in the to-do's collection so we could access the to-do's collection by saying to deuce equals database and then the same as here we can access it by its name so we can say db dot to do's or we could do it like a dictionary access so like we access a key but let's do it like this so now we have our to do's collection and now in order to put this to do item inside our collection we say results equals and then to dose and then in order to insert one element we use dot insert underscore 1 and then our to-do's sorry our to-do one so now let's run this and our code was successful so let's open our database again and go to the collections and then here we see in our to do's collection we have our one item with the name patrick and the text first to do so this worked and here we have the text we see this is an array so we can expand this and then we get python and coding and we also see that we get this underscore id so this was generated for us this is a unique identifier and this is an object id so we could put this manually if we give it here the key underscore id and then we can for example say one and then two and three but then we have to take care of this ourselves so we actually don't have to do this so this will make sure that this is a unique id so inserting works so let me show you how you can insert multiple items or multiple documents so for this let me copy and paste two more to do items so here this is a list and then here again we have a dictionary and here um we define a second to do and a third to do from a different user and now let's comment this out and now in order to insert multiple ones we say results equals deduce dot insert and now not insert one but insert underscore many and then we give it this list i call this to do's 2. so let's run this again and again our code worked and now let's go back to the site and refresh the database and now we see we get three items here so all the three to do's so now let's see how we could read three items again so let me comment this out again so in order to retrieve items or query items there are find methods so we could say result equals and then to do's and then by the way notice that we always use the collection to do this not the database so we say to do's and then we can say find underscore 1 this will just give you the first match so the first item so we can print the result and see how this looks like so let's run this and here we see we just get the first item with my first to do so this worked now in order to find particular ones what you want to put in here is you want to put in a dictionary so like this and then for example we can query for the name patrick so let's just put in this key and you might see that we have two to do's with the patrick name so let's run this and now here we get the same result so for find one will return the first match so this is still my first to do so for example we can put in more information in here so we could also use text and then my second to do so let's put this in here a comma and then this so let's run this as well and now you should see the result is the second to do all right so this worked and now let me show you what i mentioned in the beginning that we could also very easily query for those nested documents so here we have the tags and then this array so we could also very easily query for the tag python and this will give us the corresponding document so we could say find one and then here we use um this is called the key is text and then here we just use the key let's use c plus plus so we only have one item with a c plus plus key so this is this to do item so let's run this and we see this was fast and this worked as well so here we get this to do from mary so now let me show you one more example of defined one and this is um if we know the particular id underscore id then we can also query for this so a lot of times in real world applications when you work with the front end and the back end then you somehow retrieve the id and you want to query your database so let me copy this string and let's see what happens so if i use underscore id as key and then the object id and run this then we see we actually get none as a result and this might surprise you and the reason for this is that um if you have a look at the database then you see this is a object id and this is not the same as a string so in order to actually use this what we want to do here is we want to save from bson or binary json dot and by the way this is installed by default with mongodb with pi i think so you don't have to install this extra so we can say from bson dot object id we want to import the object id and then here we want to create an object id with this string like so so now let's run it again and now this works so now we get this to do from mary so yeah keep this in mind that you want to use the object id and not the string there's a difference here so now we've seen the find one um example of course we can also just use the find example so we can say find and let's remove this again and then let's use the name patrick and query for this one so there should be two items so here let's remove this and now this should work so here we print the result so let's see how this looks like so here we see this is a pie cursor object so we could iterate over this so we can save for results in results so let's call this results in this example and then say four not from four results in results we want to print the result and then we should see the actual item so let's run this so yeah here we see we have two items patrick and now what we could also do is instead of printing this cursor object we can convert this to a list and print it like this and then run it again and here now we see we have this list which has two items in it so this worked as well but now notice that we also still have this for loop here and this doesn't print anything and the reason for this is that once we consume this item then the item is gone so this is the same as with a generator once you have accessed the item then it's no longer inside so again keep this in mind so we could for example remove this and then we could use two for loops and then you might notice that we only see the results in one loop so yeah again here only two items with patrick and then in the second loop the results is empty now let's see how we can count documents so for this let's say print and then we want to say to do's and then count underscore documents and then if we run this then this should be the total number and now we see we get one type error it's missing one required argument so it needs at least one argument and if we want to just count all documents then we want to put in a empty dictionary in here so like so just two curly braces and now let's run it again and then we see we have three documents in total in here and then again we can query for different things so we can query for example for text and then c plus plus and now if we run this then we get one item and let's use a typo for example here then we should get zero items so yeah this is how we count documents let's see how we can do range queries and sorting so notice that two times in our items we just used utc now and for this last item we defined a particular date um at the january 1st 2021 at 10 45 a.m so let's see how we can query for a range so for this let's first create a date time so let's call this just d and this is datetime.datetime and then let's use 2021 and the month let's use february and then the first and now let's say results equals to dose dot find and then again here we put in our key that we want to look up and here as a key we want to say date and then a colon and then another curly braces and a string and here let's use a dollar sign and then lt this stands for less than then a colon again and then our date time that we just defined here so let's see how this looks like so now if we print and let's say or let's use a loop again and say for results result in results we want to print the result so now let's run this and here we get only items with a date less than so before this date so before february and this one is the only one in january because right now i don't know when you're watching this video but we are in may 2021 so now it's only returning this and for example if we want all dates after this then we can use chi t dollar gt so this is greater than so now let's run this and then we see here we get two objects and by the way we can also immediately sort them by saying dot sort and then for example we can sort by the key we want so in this case let's just use name so it doesn't make a difference because both are patrick but it would work so yeah you can apply sorting immediately and then sort by a key and you can use these range queries and for this let's go to the official home page so again here you find different operators and here you can click on query and protection operators and then here you find the syntax for the different ones so here we used gt for greater than we can also have greater than or equal and less than and then we have some more so again i can put the link for this in the description and then you can check them out for yourself all right so now let's see how we can delete items so for this let's remove this again and then here we say results equals and then we use to do's dot remove underscore one so you see the api is very simple we can we have find insert remove and then we have underscore one or underscore many and here again we can use a dictionary and then put in the different keys and values we want so for example here let's use this special object id again so then again we need to import this so we say from bson dot object id import object id and then here as a key we use underscore id and then here we want to create a object id with a string and here we want to use this one so copy and paste so this is the second to do so let's run this and here i have a syntax error so this must be wrapped in a dictionary of course like so so let's run it again and again we get an error and yeah here i made a mistake so deleting is not called remove one but delete one so now it should work so yeah now we don't have an error so let's go back to our cloud database and refresh the site and then we see our second to do is gone so yeah this worked um of course we can also say remove delete many and then for example let's just use the name and then as name patrick so there's just one left with the name patrick but this would work and then remove all those to-do's and here again i made a typo and so notice that you don't get a syntax error or something so you get a type error but of course we want to call this delete many so let's run this again and now our code was successful so again let's refresh our site and we see we only have one left now so this worked as well and yeah so if you want to delete all at once then just put in a empty dictionary so this is very powerful operation to delete all but now i don't want to do this now i want to show you one more thing and this is how we update the items so for this we have the update one and update many functions so let's see how we can do this so for this again we say result equals to do stop update underscore one and then again we define a dictionary so we only have this one left so let's query for text and then this should be c plus plus and now after this we have to define with what we have to update this so here we can define a comma and then we put in another dictionary and then as a first string we have to put in the operator so in order to set a different value we want to say dollar set and then a colon and then another dictionary so be very careful here and now we want to define the new value and also the key for this value so in this say example let's say we want to set this status to closed or done so then let's copy and paste this here so here we set status and then let's say done and now let's run this all right so the code was successful and then again let's refresh the page so now it's still open and i click on refresh and now we see the status is done so this might be very small for you let's make this larger and then we see this worked so this is how we update um items and again for this syntax here you can i can point you to the official site so for the operators you also have this um site with the update operators and here you see um we use this set so this sets the value of a field in a document but for example you have also these different update operators with min max and mall and so on and also this maybe might be also important so unset this removes the specified field from a document so in order to unset this we use dollar onset and then let's say status and then we have to give it a value in this example just use none and run this and the code worked so let's go back to our cluster and refresh the page and now you see the text status is gone so now we remove this um key value pair and now in order to set this again we could set status and then let's give it a new status new open so just for example and now if you use the set function or the set operator on a value that is currently not there then it will add this to this document so now if we refresh this then again we should have this status key here and here you can see for example that the mongodb is very flexible so you can remove yeah so here we see the status new open so we can remove and add new values or key value pairs and work with them so we don't have to know them in advance before we define our database we can just change them when our data changes and yeah this makes it really cool and really easy to work with and yeah these are the most important functions that i wanted to show you and hopefully you can see that the api is really easy and fun to work with and i hope you enjoyed this tutorial and again if so then please leave me a like and consider subscribing to the channel and then i hope to see you in the next video bye you
Info
Channel: Python Engineer
Views: 6,379
Rating: 4.9542856 out of 5
Keywords: Python
Id: qWYx5neOh2s
Channel Id: undefined
Length: 38min 20sec (2300 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.