Complete Python + MongoDB tutorial: PyMongo in-depth [for beginners]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay hello everyone and welcome to this new video tutorial this tutorial is primarily concerned with MongoDB with Python so we're using the Python driver for MongoDB which is PI so this is not pure MongoDB as you may have already seen in previous videos of minuses related to Python so anyways without further ado let's get started so the MongoDB driver that we're using is PI so we have to obviously import it but before we do that we must install it using tip so simply to install it you have to go to your terminal and you have to say install PI alright now we wait for a pip to install and my requirement is already satisfied because I already have pi in this project or in my PC so anyways how to get started the first thing we do is import PI alright and then we have to initialize a client for a MongoDB so we need a client to interact with the database so just to have an overview for the database this is MongoDB shell I am connected to localhost so this is data stored on my machine this is not stored on any cloud and it's not related to any online data it's on my machine so we're going to be using using the sample database my database anyways moving on so we need the client connect it to localhost to be able to see the databases there so what we do is we say client equals PI dot mango client and that's how we instantiate a new client for our application so we want to create a database so to in order to create a database before using the mighty my database I have this sample one so to create a database what we have to do is we have to say my DBA is equal to client sub and then we could call it anything so my DV alright so here client has these different databases and we weren't trying to have a new database by the name my DB so simply all we have to do right now is to run it so we go ahead and we run it okay so we run pymongo demo which is our script alright so the process was finished now we should go see a new database so going back to localhost we should be able to see a new database right here however we cannot see it why is that because as you may or may not know along with you you will not show you a new database unless there is a collection created within it so the database exists but also doesn't because you cannot define a database in MongoDB unless you have created a collection inside it so let's go ahead and do that so we want to go and create a new collection and let's call it my collection and we can say it's my DB and then you give it a collection name so let's call this collection people and we'll store in it people data then we add some data into this collection so we say data is equal to and you can only define data to be added to MongoDB through dicts or somehow similar to JSON format so what you do is that you would have a dictionary and could say name and you have seen that name that person John and then it doesn't matter single or double quotes it does not matter so ages 13 and by doing so you do you just my Col dot insert one and you say data all right so now we can run it okay now we reload and now we have my DB so it's right here so this is our sample database so we're gonna go to it and we have people and people contains one document and this document is the one we just inserted so John with ages 30 all right so this is the sort of preliminary part of pymongo so you now know how to create a database create a collection add some data to our collection you can do insert many as well so have a data list these are just variable names and then you could say okay name Jane her age is 40 and you can have okay one thing that's important to note is that within Python you cannot have field names without the quotation marks although you can do that in MongoDB shell out compass so anyways so make sure to put the quotation marks so another with name is Mark alright I'm not gonna put an H for that one so now I have a list or a array of different objects and these objects are the documents we are going to add into the database so now we just have to Michael without insert many and we say data list all right so let's just delete this one right here and we can okay so now we can go ahead and insert so running it again alright so we're done we just reload this database and now you can see that we just added two more so now we know how to insert one and insert many all right so one another thing we can do is that we can check the IDS of what we just inserted so as you can see we just specify the name and the age for Jane and the name for mark we did not specify the ID field for any of these three documents so in order to actually see those IDs I would have to add an extra line to my so I need to store this in a variable so let's say X no okay so let's say X and then here I would print X dot inserted IDs okay so this is how I would access the ideas of the things that I just inserted so running it again and this is how I get the ID so I have a list of ID's that I just inserted okay so now we know how to see how to see the IDs for the day that you just inserted you know how to insert data and how to create a first database and a first collection okay so if I didn't want to actually see I didn't want actually make my own database I want to create a I'm sorry I want to see the already available databases so it's really straightforward all you have to do is print client dot list database name so the syntax is very very human readable very easy so you know you want to list database names and now I have the names of the databases which are the same but I have an compass alright so this is basically it for the basics now what you want to do next is that you want to wait before we do that let's check also the collection names within a specific database so my DB is client so let's say my database which is the sample I said we would be using so it's this sample database and it has all these collections which are sample data alright so I have these here and I have my DB and then I want to print out all the collection name so I would just print let's just delete this from here so I would print my DB dot list collection names and now I would run it and I would get the names of all the collections that we have within this document ok so straightforward so you know how to get the names you know how to create them now so you know pretty much creating and sort of just getting to know what you have so the next thing you want to do is that you want to query your database so you want to be able to write queries and you want to be able to filter things and get certain documents so the first thing that we want to do is I want to specify one collection that we're using so my collection is my DD sub product so this will be the sample database that we're using so going back here we have products and has 11 different documents about some phone or charger products or just some electronics alright so we have this database we just initialize a collection to access this now we want to be able to find or filter some Dawkins within it so to print everything all the back all the documents we have in a collection you just have to iterate over Michael and Michael dot point and we just have to print X all right so this is how I would print every single document existing in product so there they are these are all the documents that we have everything else is simply adding conditions year so now I want to add conditions we have conditions using the document condition or matching operator which have we have used before so if you have seen any of my videos but if you haven't you maybe you're familiar with maybe or not but this is where conditions are written within defined function for MongoDB and this is where you could state that maybe you want a let's see I saw okay yeah so you want a monthly price greater or equal to 60 so this one's for it you won't be able to return it so here's monthly underscore price and we want it to be greater or equal to 60 does this okay all right so running it again now we get only two different documents and these are the two documents that I have monthly price greater or equal to 60 so this is how you query so all the different types of query operators all different types of conditions that you know for MongoDB this is where you would put them so if you worked with compass when you write conditions here when you say here type is service and then you just search any get service documents will type out service this is essentially the same thing you're replacing this condition right here with that one so this is where the condition document goes and oh yeah okay so we forgot the quotation marks so I already told you that we don't usually put these in shell and compass but you have to put them in Python because this is Python it can take them as Python variables so okay we have to put the quotation marks and these are the three different documents with service so this is how you would do it we also learned previously how to do projection or it may be you know or maybe you don't so here we are so to project something you just have to mention what fields you want to see versus what verses the ones you don't so you either say I want to see all fields except these or I want to see all the fields I mean I won't see none of the fields and only series so let's say we only want to see name and type so we just say name is one and type is one all right so it's the same documents that we just saw but only showing us name and type you can also set ID to zero ID by default is as one and going there and now we have name and type alright so this is essentially how projection works so just a reminder or a quick summary you if you say one when you want documents to show I want you want a few things to show so you want to see name and type if you don't want to see name and type you just set these to zero and then everything else would show except name and type alright so that's how kind of how you do it here's the thing you can't mix ones with zeros unless it's the ID field so you can't say name is one and type is zero that will just yield an adder with MongoDB because you can't say you have to decide whether you're doing inclusion or exclusion only ID can vary because it's kind of a default randomly generated think so you want to be able to have some control on whether or not you show it so that's essentially how you query so that's essentially how your MongoDB pymongo driver works so now you know how to kind of deal with these databases how to perform queries how to do everything so everything we just learned there was pretty comprehensive [Music] so everything we just learned was pretty comprehensive if you want more on querying if you want to understand different conditions and operators more in-depth query I suggest you refer to my MongoDB tutorials playlist where I taught functions the way we use them in pure MongoDB so shell or campus but you can take these and apply the exact same thing to PI so if you have any questions if you have any suggestions please let me know and thank you very much for watching and stay tuned for the next video
Info
Channel: Code First with Hala
Views: 32,636
Rating: undefined out of 5
Keywords: mongodb, mongodb tutorial, mongo, mongotutorial, mongo compass, mongodb basics, mongodb for beginners, mongodb queries, mongodb query, mongodb querying, querying data, databases, nosql database, nosql, pymongo, python tutorial
Id: YbLzV90dksE
Channel Id: undefined
Length: 13min 38sec (818 seconds)
Published: Tue Jun 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.