MongoDB + Python #1 - CRUD, Relationships and More

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video i'll be showing you how to use mongodb in python specifically i'll be showing you how to set up a remote mongodb database using mongodb atlas i'll show you how to connect to that database in python i'll explain the mongodb document model and i'll demonstrate the crud operations like creating reading updating and deleting after that i'll briefly discuss relationships using document embedding and foreign keys and that is what i will talk about in this video this tutorial is designed for beginners no advanced knowledge of python is required and with that said let's go ahead and get into it so let's begin here by talking about what mongodb is mongodb is a nosql or nosql document oriented database that is extremely fast and easy to use it's personally one of my favorite databases and i especially love to use it for my hobby projects that i want to get up and running as fast as possible mongodb provides a full suite of developer friendly tools like mongodb compass mongodb shell and a great vs code extension generally speaking it's one of the simplest databases to learn and has a very low barrier to entry it's also highly scalable and performant and can be used in enterprise grade applications so all of these facts are why i was super happy to have mongodb actually sponsor this video and team up with them to create this fully guided tutorial as well as some additional ones that will be posted on the channel shortly now mongodb is free to use with a basic configuration and can be hosted in the cloud or locally however since mongodb has sponsored this video they're going to provide all of you with an additional 25 in free atlas credits which you can redeem by clicking the link in the description and using the code mkt hyphen tim now that we have a general introduction to mongodb let's create and connect to our first database so as i mentioned the first step for us in this video is to create a remote mongodb database we're going to host this using something called mongodb atlas which is completely free and you can find the link to start doing that from the description now as a reminder if you do want some free credit for mongodb maybe you want to play around with it after this tutorial then use the code mkt tim that will be in the description as well as up on the screen anyways the page that i'm on right here is what's linked in the description gives you some information about using mongodb in python actually pretty helpful uh but i'll walk you through all of these steps anyways so what we need to do here is either sign in or click on try for free depending on if you have an account or not i'm gonna walk you through the setup steps from a brand new account let's do that now so i've just created a new mongodb account here i just signed in with google of course you can sign in however you'd like and i'm just going to go through the privacy policy and all of this type of stuff and kind of walk you through what we need to do here to get our first cluster and database set up so you'll be brought to a page like this that kind of asks you some information so i'm going to say i'm going to learn mongodb i'm curious if they have kind of education in here we'll just go with other for now i'll say education uh what's your preferred language and then we'll go with python now once we fill that out we're going to be brought to a page like this we want to select the free plan for now of course if you want a dedicated one then you can go for that and i'm going to click on create and for now we're going to make sure that it's shared that's what's free and we're just going to go through a few configuration options here so anything that doesn't have the little i guess dollar bill beside it is free so depending on what you're closest to select your region in my case i am probably going to be us east because i'm kind of in toronto region and they don't have a canadian one right here that's fine and then for your cloud provider i'll just go with aws it doesn't really matter all this type of stuff you don't really need to change and you actually can't really change most of it because we're just going with the free plan here but for the cluster name you can name this whatever you want in this case i'm going to name it tutorial and as it says here you can't change this once it's named so just give it a name that you're going to like and want to keep but again this is for demo purposes so not a huge deal okay so i'm going to click on create cluster here that's going to take a second but first i need to enter a username and password that i can use to sign into my cluster so obviously don't forget what this information is i'm going to go with tech with tim and then i will type out a password here that i'm not going to tell you and create my user okay my user has now been created and it's going to ask where we want to connect from so local environment or cloud environment in this case we're going to connect from our local environment and to be able to do that we need to add our ip address to the allowed ip address kind of access list so they actually make it really easy here you can just click on this button i'm going to blur this out because obviously i don't want to leak my ip address but i'm going to add this to this list so i'm going to say add my current ip address that is now added and then we can click finish on finish and closer and go back to databases now that we are here we just have to wait for the cluster to be created once it is i will be right back and walk you through the next steps so my cluster has now been created you can see tutorial right here and i can click on it to view the overview page and i just want to mention that what a cluster is is kind of an abstraction on top of a database not exactly but it is a higher level than a database databases would be inside of a cluster and in this case the type of cluster i have is a replica set with three nodes now what that means is that i actually have redundancy here because i have three servers that are running my database so my database is going to exist in three different places and this is going to provide better scalability as well as redundancy right in case any of the servers go down or in case we lose some data we'll have that backed up in multiple places again i don't want to get into this too much it's a bit more advanced than what i want to talk about in this tutorial but i just figured i'd mention a cluster again it's kind of on top of a database provides scalability as well as redundancy and a few other things for now though let's click into tutorial and you can see there's all kinds of stuff that we can have a look at here and we can click on connect now connect is what i want to go to because we need to grab our connection string and figure out how to connect here so we can connect our application we can connect using mongodb compass which is a developer tool and we can use the mongodb shell same thing that is a developer tool for now let's go with connect your application and we're going to select python here and we'll go version i guess 3.11 or later shouldn't actually make a big difference for the connection string here okay so now that we've done that it's telling us that this is the string we're going to use to connect to our our cluster and to connect to our database so we can copy this and we'll bring this into a python file now i just want to mention quickly that there is a program here called cli so the command line interface you can do everything that i just did from the cli but it's easier to go through the graphical user interface setup if you're not familiar with this so if you care to you can download the cli i'll leave a link to this in the description and again you can create a cluster create a database collections all that type of stuff from the command line you don't need to do it from the graphical user interface anyways we've pretty much done what we need here for setting up our remote database so let's go into visual studio code which is the editor i'm going to use for this video and let's just paste our connection string right here into a python file i will deal with it in a minute but we just want to have access to that for later on so now that we're here in visual studio code i'm just going to open up my terminal here and start installing some of the dependencies that we need for this project the first thing that we're gonna need is what's known as the pi module in python and it's going to allow us to easily interact with our database so we just want to go into a terminal or a command prompt and type the following command pip install and then this is going to be pi and inside of square brackets you're going to put srv like that so try this command i should already have this installed so it should happen pretty quickly and then once we do that we're going to install one more module as well so the next module is going to be python hyphen dot env this is going to allow us to access an environment variable file where we're going to store our credentials for signing into mongodb okay so those are the two pip commands that we need to run if for some reason those commands don't work for you you can try the command pip 3 you can also try python hyphen m pip install or python 3 hyphen m pip install depending on your operating system try all of those and if none of those work i'll leave two videos on the screen and linked in the description that show you how to fix this commit okay now that we have that installed though we're just gonna set up vs code to be better for working with mongodb so there's actually an extension in vs code if you go to the extensions pane here for mongodb so if you just type in mongodb you'll see the extension by the verified creator here or author and you can install this i already have it installed now this extension provides a bunch of awesome features where we're able to navigate our data actually view it here and visualize it we can go into a playground mess around with some queries uh we can do a bunch of other stuff like document editing very useful and definitely something you want if you're going to be working with mongodb once you install this you probably need to refresh vs code then you'll see a little button here for the extension where you can actually connect to your cluster which we'll do in a second as well as go to the playground okay so now that we've done all of that let's actually connect because we want to start writing some code here for mongodb so for now i'm just going to say my connection underscore string is equal to and then i'm just going to put this in a string and when i save hopefully it's going to put that on a new line okay it's not so let me just separate this a bit just so we're able to read it and i'll go with a triple quoted string here i just make that a little bit easier to see okay so that's my connection string but you'll see here that we need a password so what i'm going to do is make an environment variable file here so just follow along and make a new file called env make sure this file is in the same directory as where your python file is and inside of here we just want to make a variable i'm going to call this mongodb underscore pwd standing for password i'm going to make this equal to whatever the password is now of course i don't want to show you what this password is so i won't type it in right now but place your password in a dot env file uh that has under mongodb sorry underscore pwd equal to and then whatever the password is in a string once you've done that we can go to main.pi and we're going to use that python.enb module to load that environment variable file and get access to our password without leaking it directly inside of the code so to do this i'm going to say from and this is going to be dot env import and we're going to import load.env we're also going to import find.env so the first thing we'll do here is say load underscore.env we're then going to put inside of here find.env and this is kind of a shortcut that's just going to load the environment variable file for you so you don't have to manually define the path and all of that now before we do that we'll import a few other things we're going to import os we're going to import p print which stands for pretty printer we'll have a look at that in a minute and then we're going to say from hi and we're going to import the client which we'll use to make a connection okay now that we've done that let's grab the password from our environment variable the way we can do that is say the password is equal to os donevyron.get and then we're going to put the corresponding name that we had in our env file so it's going to be mongodb underscore pwd that will then give us the password that's stored inside of here alright so now that we've done that we're just going to change this string here to an f string only available in python 3.6 and above by the way and i'm going to put my curly braces and insert my password into the connection string now that we have that we can actually connect so i'm going to say my client is equal to the client and then i'm going to pass the connection string like that so at this point we're pretty much ready to test this code out and see if it's working however i do need to make a quick fix here i realized that this says my first database now that's just the default option that it gives you for mongodb we don't have this database created yet so if we try to connect to it there's going to be an error so i'm just going to remove that and i'm actually going to move all this up onto the same line just because i think it's going to give us an error if it's not all on the same line so that means we can kind of remove the triple quoted string here and make it a single quoted string and now that should be all good so let's test this code out by just running it and if we don't get any errors here that's good that means that we were able to connect and it looks like everything's happening fine we're not getting any errors great now that we've done that i want to show you a graphical user interface tool called mongodb compass that allows you to actually visualize the database and is going to be really helpful to us now again we can do this from the vs code extension but compass will work even if you're not in vs code so i figured i'd show it to you this is what the application looks like however if you want to download it i will leave a link so you have compass right here another way to get to kind of the download of mongodb compass is to go to your cluster click on connect click down here where it says compass and then select either i don't have compass or i do and you're gonna have to go here anyways because what we want to copy is the connection string right here so copy the connection string then open mongodb compass so we're gonna copy that here we're gonna go to compass i'm assuming at this point you would have ran through the installer if you're gonna download this and you should be able to simply paste this in so i'm going to say are you sure you want to edit your connection string yes i would like to edit it i'm going to paste in this connection i'm going to write out my password editors please blur this and now that we have that we can click on connect all right so i've just connected here to the database if this doesn't work again make sure your ip address is in that access list you can get there by clicking on network access from the side panel here and then add your ip address now though we see that we have two databases admin and local so we can create a database manually here or we can do one from code or additionally we can do one from the cli tool as you can probably tell mongodb has a ton of pieces of software to help developers out and make it easier than doing it all manually from code so why don't we just make a database while we're here we can call it test and then we're going to create something known as a collection which i'll discuss in a second and we'll call that test as well okay so let's create that takes a second we now see that we have tests and if i click in here we have a collection and inside of here we'd be able to view all of our documents again i'm going to discuss what that means all right so now that we've done that let's simply see if we can view some information about our database from our code and then i will talk to you about the mongodb document model and kind of how it's different than sql so for now what we can do is say that our databases so dbs is equal to and then we can use our client and we can use the method here list database and i think it's just going to be database names like that so for now let's just print this out we'll print out the dbs and let's see what we get and notice here we have three databases test admin and local now if we want to access a database it's pretty easy to do this i can say my testdb is equal to and then this is going to be the client dot and you just put the name of the database so i can do test now you also i believe can access it like this so using kind of the dictionary or key syntax but usually i prefer just to type it out like this okay so client.test and what we can do is list all of the collections inside of this database and the way we do that is say collections is equal to and then this would be the test underscore database and then not get but this is going to be list underscore collection names like that and then of course i can print out collections so let's run that and let's see what collections we have and notice we have one collection test which is the one that we just created okay now that we've talked about that let me just hop over i guess to another screen and discuss the document model and then we'll come back here and look at the crud operations so now that we've created our first database let's briefly discuss the differences between mongodb in a traditional database so most databases are known as relational database management systems or rdbms for short and they source structured data in something called tables now they usually use a language called sql or sql standing for structured query language to perform modifications to data and enforce a strict set of rules for your data there's plenty of merit to traditional sql databases but for us python users mongodb being a nosql database can be much more flexible and easier to work with so a nosql database like mongodb actually stores unstructured data and it's typically in json format now this allows us as developers to perform frequent updates and changes to the structure of our data and often use more familiar types and not be kind of restricted by some of the bounds of a sql database now nosql databases also can typically perform faster queries and scale out more easily just based on the way that they're designed so now that we understand some of the core differences let's talk about mongodb and something called the document model so mongodb has databases a database is made up of collections and a collection is made up of documents now the documents store all of your data that data is categorized into collections and the collections make up a single database so to give you a concrete example if you were building a database for a library you may have the following collections a book a person maybe a rental for like taking out a book and within each of those collections you would have documents which actually store the data related to the collection so in the book collection you may have a document that stores an author title publish date maybe the number of copies available and a bunch of other data as well if you wanted to and then continuing here the data in your documents is stored in a field value pair so it's kind of just like a dictionary in python you have a field that field has a value that's what you're placing inside of your documents now this means you can store things like arrays sub documents and any other valid bson type so internally mongodb actually uses something called bson uh it's similar to json but it does have some minor differences and i'll put up on the screen here the documentation and as i scroll through you can kind of see some of the types that are available to us here in vsan so with that said that's really all you need to know about mongodb to get started so let's start creating some data and learning mongodb alright so hopefully that was helpful and gave you some insight into how mongodb works also gave you some of that core terminology now that we understand that let's look at a quick example of inserting a document so actually creating a document inserting it into one of our collections again the collections you can kind of think of as tables right or a place where we are collecting information collecting different documents so here what i'm going to write is just a function just to keep everything nice and neat i'm going to say insert and then this will be the test underscore document and then what we want is to get access to the test collection from our test database so i'm going to say my collection is equal to my test database so test underscore db and then to access a collection just like we access the databases we use the dot notation and i put the name of the collection and we know we have one called test so testdb dot test now i'm accessing a collection and once i have a collection it's very easy to insert a document into it so we're going to make a test document here and really the documents we're just going to use python dictionaries to represent them as i talked about they're very similar to json internally mongodb uses something called bson but what will happen is any types that we have here will automatically be converted into the correct mongodb types so we don't have to worry about specifying some exist string or an int or a float it just automatically gets converted into whatever type mongodb can handle if it can handle the specific type that we have so for now let's do something really simple like just have a name and then we can have maybe a type along with this and the type we can just do something like test okay so from here we are now able to insert this document and the way we do that is we say collection dot and then we're going to use the method insert one and we just pass the document that we want to insert so we do test document like that now one thing we may want to access with this document is its id so every single document in our collection is going to have a unique id you can kind of think of this similarly to the primary key that you might have in a sql database and that is the unique identifier of this document so if we wanted to access that so inserted underscore id we could say inserted id is equal to this dot inserted id and that simply gives us the id of the document that was just inserted into the collection so what i'll do is just print insert id just so we can verify that this is working and we're getting some output and now let's simply call this function so insert test document like that let's save let's run our code and let's see what we get and notice we get this uh this string here telling us what the insert id was now this id is what's known as a bson object id this is actually a specific type i'll show you how we kind of work with those in a few minutes but just keep that in mind it's kind of a unique type it's not just an integer all right so now that we've done that let's go back to compass here and let's refresh where is refresh and notice that i have a document now inserted it automatically has this underscore id field again that's the unique identifier here and then we have any of the information that we added like name and type great so super easy uh simplest you know possible way to insert something into a database so now that we've done that let's have a look at more insert operations and actually creating a database or a collection here from code so to make a new database is super simple and same with collections and documents and all that but i can do something like let's go with production and we'll just call this database production just to have you know kind of a unique name as opposed to test so i'm going to say production is equal to client.production now when i do this i try to access a database that doesn't currently exist mongodb is automatically going to make that database for me so very straightforward we now have that and then after we do that we can create a collection and in this case i'm going to call it the person collection so i'm going to use my production database and access the person collection and again similarly to here since person collection doesn't exist it will automatically be created then what we need to do is insert a document because if we don't insert anything it actually won't make this we need to insert a document first so let's write a function here and say define create underscore documents and let's actually see how we can create multiple documents at the same time because i already showed you how to insert one okay so we're going to say first underscore names and in fact rather than me typing this all out let me just copy it in and save us a bit of time so we're going to say first names is equal to this last name is equal to this and ages are equal to these just having some corresponding values all of these are the same length and then we're going to loop through all of these create a document of them and then simply add that document so what i can do here is say for and we can say first underscore name last underscore name age in zip and we're going to zip all of these so first names last names and age when you zip it's just going to give us all of these items at the corresponding indices in a tuple kind of a useful function in python if you haven't seen that before and then we can say our document is equal to and we can go with first underscore name is equal to first name last name is last name and h is age thank you very much autocomplete and then we can do something as simple as personcollection.insert1 and we can insert the document now that's one way that we can insert multiple documents we can just loop through but a more efficient way would be to do the following so i can create a list of documents by saying docs dot append and then appending my individual documents and then rather than inserting them one at a time we can simply use the insert menu so i can say insert many i can pass a list of all the documents i want to insert and then that'll do it for so in fact let's do that and let's call create documents here and let's just test this out and see if it works okay so i'm going to run the code give that a second and this is local variable age reference before assignment sorry need to change that to be ages not age let's rerun and there we are okay so now if we want to verify that worked we can go to compass we can have a refresh here we now see we have a new database called production we have a person collection we have a few people that were now inserted inside of here because of the code that we wrote all right easy enough that is how you insert documents in and i just want to show you here the comparison to if we were to write the sql queries to do this just so you get a real idea of how flexible and easy this is compared to working with sql databases so if we wanted to do this in sql i'm just going to paste this in here we would need to write the following first so this would be i guess the query or the creation string that we need to write to create the table person which is kind of the document that we that we just created right we had uh our person collection and really i probably should just call that person but that's fine anyways we had to create this table in our sql database and then if we want to insert something into the database we would have had to write this string right here so not overly complicated but definitely not as intuitive and simple as this and something i prefer to stay away from if i can writing complex sql queries anyways there you go that's the comparison in terms of the sql queries now let's move on and look for how we can query documents because obviously we want to be able to look through our database and find some different elements so what i'm going to do now is write a series of queries that we may want to perform on our database or i guess in this case our collection to find some different documents i'll kind of go from easy to more difficult and i will mention that if you're interested in more advanced queries then stay tuned for the next video in this series where that's really what i'll be focused on for now though let's write a function that just says find all people and we'll see how we can just get all of the documents that are in our collection now to do this is pretty straightforward i'm just going to say people is equal to and then this is going to be the person collection and not find all but dot find now dot find allows you to insert a query object which is different properties that you're trying to match with when you're looking for elements or looking for documents sorry in this case if you leave this empty it's just going to find all of the people or all the documents in the collection so what i can do now is say for person in people and i can loop through this and i'm just going to use something here called the pretty printer to print this out and the way we're going to do that is we're going to say our printer is equal to print dot pretty printer now recall that i imported p print up here that's why i'm able to do this and rather than using the standard print statement i'm going to say printer dot p print in print out person just because it's going to give us a nicer formatted output now that we've done that we can call this function so find all people and test this out and see if it works and i'm just looking through the rest of the code to make sure we haven't made any mistakes up there and it looks good to me okay so let's run this and notice now that when i print this out i get some better formatted output than our traditional print statement and you can see i have all of the documents that are in my person collection that we just inserted i get them here and i can treat these as python dictionaries i have key access and everything you would know about a python dictionary applies here great now one thing to note here with people if i try to print out people you're going to see that we get kind of a strange result here we get a pie cursor now the cursor is something that you can iterate over so you can't directly access or index say the cursor you can iterate over it in this way or if you just want to get all the elements from it and store them somewhere you could print out say the list of the cursor so if i do this here you see when i print that out i get a list of all of the different people so i just kind of wanted to show that to you there is you know a unique way of dealing with the pie cursor really it's going to act as a generator or an iterator but i imagine many of you are probably not familiar with that so that's fine okay so now that we have find all people i'll show you again that the equivalent sql query to do this would have been select asterisks in person not overly complicated but this seems a bit more intuitive to me the next thing i'll show you how to do is look for a specific document based on a field value so maybe we want to search for a first name equal to say tim or something like that right so to do that let's write a function i'll call this findtim because i want to find myself in our database and let's see how we do that i'm going to say tim is equal to person collection rather than find i'm going to do find one and then inside of here i'm going to put the field as well as the value or the fields and the values that i want to access from my collection so in this case i'm going to say first name and i want the value of first name to be tim for anything that i select here so i'm saying i want to find one and the first document that i find that matches this criteria where first name is equivalent to tim will be returned to me and then again i can use my printer and p print this out and have a look at it so let's call find tim let's run this code and notice here that i get the document associated with tim now usually you want to search for documents using their id sometimes you don't know the id like in this case i don't know what the idea is so instead i have to use a field now i could also specify multiple fields here and do something like last name and then go with my last name and assuming this matches the document i would get it uh but these all have to match it's not like in either or it's any field you specify here the value needs to match exactly for this to be returned to you now you can do the same thing in find just the regular find command and then it will return all of the documents that match that criteria for now they will go back to what i had before okay so now that we've done that let's have a look at how we can do something maybe a bit more advanced i will also again show you the equivalent sql query for this would be something like this select star from person where first name is equal to tim okay let's get rid of that next though let's write something that can count all of the people that we have so we'll call this count all people and inside of here we can say the count is equal to and then there's a few different ways to do this but i'm going to say person collection dot count underscore documents like this and then i can pass an optional filter here which again is going to be something similar to this which will only count documents that match the filter now if i leave the filter empty then of course it's going to count all of the documents because everything will match an empty filter anyways though if i'd like now i can print something like of people and then count and if we call this count all people and run the code uh you'll see that we get the count so there's six people uh or six documents really in our collection okay so that is one way to count another way to count is to do the following we could say couch is equal to person collection dot find and then we can say dot count in dot count will essentially give us the length or the number of elements that were returned by this find so you don't have to count all of the elements in this case you can count all of the ones you know returned by the find but we should see that we get the same result here as six when we run this uh and yes we do okay uh it's actually telling us that this is deprecated so maybe we should use what i did before count documents but still count can be useful in some context okay now that we have done that let's write something that can find a person by their id now this seems trivial because i've showed you how to search by a field name already we actually need to do something specific here for this to work so let's write a function this is going to be get person by underscore id and inside of here let's take a person underscore id okay so now we're going to say from and this is going to be bson dot object id import and then object id like that now bson i believe is built into python so i don't think you actually need to install that if for some reason that doesn't work then you can do pip install bson although i imagine that's going to work for all of you and from here we will now create an id that is equal to the object id that wasn't exactly what i meant to describe but you'll see what i mean so i'm going to say id is equal to object id person id i'm just putting an underscore here so i don't shadow the built-in id function in python and this is what i was talking about previously is that our ids when they get returned to us or when we kind of punch them in here they're going to be strings in python but we need them to be this special object which is object id otherwise they're not going to work when we try to search for them in our mongodb database so keep that in mind it's kind of the one quirky thing you need to convert all of your ids to object id objects from bsun okay so now that we've done that i'm going to say person is equal to the person collection and then we're going to say not find one and now we're going to query by the underscore id and we're going to pass while the underscore id which is the bsun object then we can say printer dot p print and we can print out person and we can call this function so let's say get person by id we just need to grab an id so let's grab that from mongodb compass let's find the id here of say sarah so let's copy that in and paste that here notice we have a string it will then be converted for us you could put this import statement somewhere else i'm just putting it in the function so it's self-contained and now when i run you will see uh we actually got an error no built-in module p print uh oh sorry this is printer.p print okay silly mistake let's run that again and notice here that i get the object i was looking for i get sarah accessing by id wonderful okay now that we've done that again let me just show you the sql query just you guys get an idea of what you would write equivalently so if i wanted to access a person by their id i would write something like this and from the previous one because i didn't show it to you we would write something like this select count asterix from person again less intuitive and if you don't know sql probably hard for you to come up with without without doing you know a few google searches and stuff like that okay we have done that the last thing that i will show you is how to get uh kind of a range of something how to do a bit of a more advanced query and of course you can do much more advanced than what i'm going to show you here but for now let's see how we can get people that are within a specific age range so i'm going to say define get age range i'm going to take in my min age and my max age i'm going to search for only people in my collection that are within this h range now we can pick inclusively or exclusively i think here we'll go inclusively so let's actually build out our query string our query object here and i'm going to use kind of an operator from mongodb which is called dollar signed ant now there's a ton of other operators in mongodb again in the next video i will show you a ton of them you can also reference the mongodb documentation but what we can do is query multiple things using the and and if all of the queries that we write here are true uh or they match then it will return the object force so similar to kind of an and uh in you know logic right in in python when you would use the and keyword okay so we have and we're gonna pass a list here and then inside of the list we're going to pass different queries so the first query that i want to do is relate to the age well the next one will be age as well so i'm going to say age and then i'm going to pass another object inside of here and i'm going to use an operator and this operator is going to be gte standing for greater than or equal to now we also have greater than we have less than we have less than or equal to we have equal to we have a bunch of them again i will show some more to you in a later video now though we have gte and we want to say that we want the age to be greater than or equal to the min h so you kind of get got to get used to the syntax we're saying this is the field and then this is the query we want to apply on the field we don't want to check for exact equality we want to see if the minimum age or sorry if the age is greater than or equal to the minimum age that we place here okay now let's copy this because it'll be very similar for the next one we're going to say age we're going to change this to lte so less than or equal to we're going to make this max age and now this query when we use it inside of our find will return everyone in this in this range sorry so i'm going to say people is equal to person collection.find we're going to query and then we can also pass something here like dot sort and when we do this i can sort by age and then i can grab you know the sort of results here so let's now have a look at this we're going to say four person in people and we'll say printer dot p print and we will print our person out i can then call the function so get age range and let's look for people that i guess are within the age range of what ages do we actually have i guess we could do something like maybe 20 and 35 or something and that'll give us a few results okay so let's do 20 and 35 let's run our code and uh we got an issue here unhashable type dictionary my apologies guys i have an accidental um secondary kind of dictionary surrounding the and so let's save that hopefully that format's better for us okay that's better sorry i had another set of curly braces that i really didn't need and that's better okay so let's run this and see and there we go we get all of our results here and we can see that they're within their age range right so we have 21 23 and 34 sorted by age in ascending order wonderful now that we have done that again we'll show you the sql query so let's have a look at this sql query this is what it would be i actually paste it as a comment this time select asterisk some person where age is greater than min age and age less than max h great okay i lied i'm actually going to do one more find query here then we'll look at updating deleting and some relationships as well okay so the next thing i'll do is show you how we can project specific columns because sometimes when you are querying documents you don't care about all of the information you only want to get some of it so i'm going to say project columns and inside of here i'm going to create a dictionary here containing the columns that i would like to have in my result so i'm going to say underscore id and i'm going to put 0. now that indicates that i don't want to have the id in my result so don't give me the id field of any of my documents i'm then going to say my first underscore name this is gonna be one which means yes i would like to get my first name and then i will put my last underscore name and i will put that as a one as well indicating that i want the last name so now what i can do is say people is equal to person collection.find i can pass my query object here and i can also pass my columns now i don't need to pass this as a keyword argument i can pass it positionally and when i do that it's now going to do a projection which just means it's only going to give me the columns i specified right so let's just copy what we had here for printing this out okay and then let's call this so project columns let's run our code and let's see what we get down here and notice that we get our first name and last name we do not get the age and we do not get the id because we did not specify that we want those all right so that is it for querying that's really all you need to know for basic querying that should get you by if you're doing kind of a beginner or hobby project now though we want to look at how we actually update a existing document in our collection so let's write out a function here and let's call this update and let's go person and then this can be by underscore id let's again take in our person id and then from before we can copy our bson here to grab this okay so let's copy that in we have id is equal to object id person id that is what we need and now i'm going to show you how we perform updates so there is multiple ways to perform updates but the most common way is to create i'm going to call this a query object of some sort or kind of like an update object and we're going to use some operators here so the first operator we can use is set now what this is going to do is just change the value of a specific field or sorry actually this is going to set a new field to be a value of our choice now we also can use this to override an existing field but here i'm going to set kind of a new field on my document to show you how we do that so in this case i'm using the operator set i then need to pass the field that i want to set so i can do something like new field and i could give this a value of let's say true because we haven't done any booleans yet okay so that's set now if this field was something that currently existed we would override that field uh so keep that in mind when you use set all right next we're gonna have and this will be inc so this stands for increment there's a ton of operators in mongodb and we can do age and then one and this will increment the age of someone by one or whatever the document is that we're modifying i then can do another operator here and let's go with something like rename and let's rename a few fields at once actually here so what i can do is say rename and i can do something like first name and we can rename this field right we're not renaming the value we're renaming the field to be first and then we can do last underscore name and we can rename that to be last now that would work similarly here if i want to increment multiple fields i would just put the field and what i want to increment by if i wanted to set multiple fields same thing i just list that in here okay now that we have our updates to update a document we are going to use the very nicely named function here so person collection and then this is going to be dot update underscore one we need to pass who we want to update or what document we want to update now we can do that by passing in a query object here that will return to us one object so i'm going to say id and then i'm going to put my underscore id because i'm updating whatever the person id is then i will go and uh what i actually want to do for my update so i'll say all updates like that okay now that we've done that uh we can run this code for now and just have a look at what this is gonna do so let's say update person by id uh we need an id so let's grab that from compass here we can update tim so i'll just copy that in okay let's make sure we put that in a string and let's run this and then have a look at compass and see if any changes were made okay so i run the code i'm not getting any errors here which is always good news if i come back here notice the age was 21 we should see that go up to 22 and notice the new field has been added as well as the field names first and last being renamed wonderful so let's just comment this out now because i'm going to continue writing in here but i don't want to repeat this and the next thing i'm going to do is show you how we would say unset a field so actually remove something from the document so to do that we can do something like person collection and again we can use dot update1 so i guess i probably should have copied what i had before but that's fine we'll go underscore id underscore id and then we can pass inside of here uh the update we want to make and in this case i want to unset so i'm going to use the operator on onset this really kind of just stands for delete and then i want to remove the new underscore field but to be able to do this properly i just need to pass some value it doesn't really matter the value i pass here i'm just going to pass an empty string it's just because we're passing a dictionary in python so i can't just have the string there i have to have the key value pair so i have unset colon and then what do i want to unset well i want to unset the new field or yeah i guess that's it unset the new field so we put our empty string and then we are good let's now run this and hopefully it should remove that field okay so run the code looks like that works successfully let's go to compass let's refresh and notice the field is gone okay that is pretty much all i had to show you for updating a person next what we will have a look at is replacing a document so we're going to say define replace underscore one again we need that person id so let's take that in we can copy our bsun stuff from up here all right and now that we have that we want to have a document that we're going to replace a document with now you would do this if someone was updating like say all of their contact information are updating you know pretty much everything but they want to keep the same id because if you insert a new document you're going to get a new id for that no matter what so in this case uh you want to keep the same id by changing all of the other fields or while changing all the other fields that's why you would perform a replace so i'll just copy this in because it's not really worth me typing it out but we'll say new dock is equal to this this is the new stuff that we want to have and then here we're going to say person collection and we're going to say replace one and then we're going to pass id we're going to pass the underscore id like that and then we will have our new document that we want to replace this document with while maintaining the same id okay so let's now just change our function call to be replace underscore one let's run the code and let's now go back here to compass tim is the one we're going to be replacing so let's refresh and notice we have new first name new last name age 100 id stays the same wonderful all right that is it for updating and replacing documents next we are moving on to deleting alright so as i said we're going to delete documents now doing this is fairly straightforward let's write a function here we'll say delete doc by id okay uh we'll take in the person underscore id here copy our bson now this is as easy as it is we can write personcollection.delete1 this is going to be id and then we will pass inside of here underscore id so let's try this out let's go delete doc by id should have kept my id but let's go back to compass let's copy it in okay we have a string paste and run and that should delete just fine let's go back here should see this document go away now refresh and tim has been deleted all right so that's it for deleting one now if we want to delete many documents uh we can use person underscore collection dot and then rather than delete one it would be delete many and just like i showed you the queries before when we were using find you can use those inside of here to select the documents you want to delete of course same thing with delete1 but yeah delete many if you want delete multiple of them put in your query if you just did an empty query like this then it would delete every single document which i don't really want to do right now alright so as i was saying we're now going to move on and talk about relationships so relating documents together and how that works in mongodb because it's a little bit different than in a traditional sql database now when i'm talking about a relationship it's helpful to have an example so let's paste in an address here and let's just do kind of a dividing comment just so we know what we're talking about here okay so we have an address uh we might have a person right so let's actually say person is equal to and then let's create this now this is going to have an underscore id we'll put in some random value here this would have an id as well just putting these in for example purposes okay so obviously their ids will be different and then inside of person we might have something like say the first name and we can just make that john and that's all we need for right now but the idea is that this address may belong to this person and right now we don't really have a way of kind of denoting that and there's a few ways to do this in mongodb so the first way to do this is using something called document embeds now this is when you embed a document inside of another document and that would look something like this we create a new field here on the person document and we take our address document here and we just pasted it so now we have address and the address is equal to this document and this works particularly well when you only have a single piece of information that you need to kind of relate between different documents in this case we have an address we just store the address on the person all is good and in fact this is going to be slightly more efficient than storing them in two separate collections which is the alternative method so hopefully that makes a bit of sense but you can store a document inside of another document you also can store an array or a list of documents inside of other documents so if you had multiple addresses you could embed them inside of the list so that's fine however sometimes we're in a situation where it's not ideal to do that or this address maybe belongs to multiple people so if i'm going to be embedding it i have to embed it in multiple places and that can kind of complicate things there's all kinds of pros and cons which again i'm going to get going to get into sorry in a more advanced video however let's say we decide we want to store the address and the person in separate collections we want to organize them in that way for whatever reason is there still a way that i can relate this address to this person well yes there is and the way we do that is using something called a foreign key so what i can do here is add a new field to address and i can do something like owner id and i can simply make that equal to the id of whoever owns this address or whoever this address belongs to whatever you want to say so now that i do this i have a reference from my address to the person that owns the address and i can use this to perform something called joins where essentially when i'm searching for a person i'm searching for an address i use this field to determine what address belongs to what person that's hopefully pretty straightforward but those are the two methods you have you're embedding and then you have what i'm going to call i guess a relationship or you know kind of a foreign key in that sense anyways though i just wanted to quickly go through those now let's see how we actually use these these different methods to kind of relate objects or documents together so the first thing we'll do is just using an embed because that's pretty straightforward so we can say add address underscore embed here and we're going to take in a person id and an address that we want to add for that person so we need to of course grab the id so let's take this in here okay now that we have the id uh we can simply perform an update to the person and we can add a new field uh that contains this address so we're gonna say person collection and not insert one but this will be update one okay update 1 id id and then the update that we want to make here is the following so as i said before we can have a list or an array and we're actually going to use that here to store potentially multiple addresses because a single person could have multiple addresses right so if we want to be flexible rather than adding a single address let's add a list of addresses or an array of addresses as i talked about previously so i'm going to say add to set is going to be my operation here because we have the dollar sign and then i'm gonna say addresses like that colon and then address now what this says is add this address to the key addresses and when you put set here that's indicating that addresses is going to be an array now if addresses does not already exist it will be created automatically and have a single element if it does exist then we'll add another element to it so append it to the end there you go now we can run this let's say add address embed we n we need an id so let's go back to mongodb let's copy the id for sarah let's paste sarah's id and let's put in the address let's run okay and when we run assuming no errors we can go back to compass we can do a refresh and then we see we have an array inside of here we have an object and it contains the address that we just inserted okay so that's method number one now method number two is going to be the following uh and this is add address underscore relationship now really both of these are relationships but hopefully you get what i mean again we'll take in person id and address we're gonna paste that like that and inside of here we actually need to make a new collection for our addresses because as i said we're going to store in a separate collection and then relate them together so i'm going to say that my address underscore collection is equal to and then this will be actually our test uh was it test database no we had the production database right it'll be production dot and we can just call this address okay so that's our collection now that we have that we can just insert the address into the collection so we can say address uh underscore collection and not update one we're gonna say dot insert underscore one and then we will just insert the address that's passed however before we do that it's probably a good idea that we add our owner id field so we actually have a way to reference who owns the address and again we need to make sure that we convert that to an id first before we do that so we're going to say address is equal to address.copy just because i don't want to be mutating the input right here the input object so i'm going to make a copy of it and then i'll say address and i'll add a field here called owner underscore id and this will be equal to the person id so now we add that to the address so when we insert address we have owner id and there you go that's that's really all we need so let's change this now to be relationship and we'll change from sarah because they already have an address we'll go to jennifer so let's copy hers and let's paste that we'll make it the same address because why not uh let's run the code alright and let's see what we get here when we go back to compass and we do a refresh when we do this we see we now have a new collection called address and inside of here we have the address with the owner id equal to sarah alright so i think with that said and me showing you how we perform the different types of relationships i am going to end the video here i was going to show you how we perform a joint operation but it's beyond the scope of this tutorial and i'm going to show it to you in the next one and really give you some more in-depth explanations so it doesn't feel kind of like for lack of a better word half-ass explanation here anyways i hope you guys enjoyed this i put a lot of work into this video to make sure i could teach you really all of the core functionality of mongodb as easily as possible again i keep saying it if you'd like watch the next video i will show you some more advanced operations and again a massive thank you to mongodb for sponsoring this video if you haven't already you can claim that free credit by using the discount code that is in the description or i guess the credit code in the description as well as clicking that link thanks to all of you that watched if you enjoyed make sure to leave a like subscribe the channel and i will see you in another one [Music]
Info
Channel: Tech With Tim
Views: 100,349
Rating: undefined out of 5
Keywords: tech with tim, mongodb and python, how to use mongodb and python, what is mongodb, mongodb atlas, mongodb compass, python, what does CRUD stand for, CRUD operations, how to setup mongodb, pymongosetup, mongodb document model, what is the mongodb document model, how to setup mongodb atlas, fix pip, how to fix pip, mongodb cli, download mongodb, what is mongodb compass, what is mongodb atlas, tutorial on mongodb and python
Id: UpsZDGutpZc
Channel Id: undefined
Length: 54min 51sec (3291 seconds)
Published: Mon May 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.