Python MongoDB Tutorial using PyMongo
Skills:
LLM Foundations80%
Key Takeaways
This video tutorial demonstrates how to use MongoDB with Python using the PyMongo library, covering basic operations such as creating a database, adding, searching, deleting, and finding data. The tutorial also covers more advanced topics such as using regular expressions, updating documents, and counting documents that meet certain criteria.
Full Transcript
so this video is gonna show you how to use MongoDB with Python and the Python module pi now MongoDB is an awesome database it's super easy to get set up and in this video I'm gonna be showing you how you can get 500 megabytes of free data in the cloud that you can use for any applications that you want so let's get ahead go ahead and get started setting up what's known as a cluster and then we're gonna start working with the actual Python code and seeing how we can do different operations on our database retrieve information delete information add information all of that kind of fun stuff now just quickly before I move too far I just want to say that this is a beginner tutorial so I'm going to be showing you just kind of the basics of MongoDB it's capable of a lot more but if I were to show you everything this would be hours long so I'm hopefully hoping this will take about 10 to 15 minutes and the Python code I'm gonna go through will just show you the basic operation so like add search delete and find modify update that kind of basic stuff so that being said let's get started so we're gonna have to head over to MongoDB comm and create some kind of account here now I already got one but what you guys need to do is just go ahead and click sign up or try for free and then it should bring you to a page that looks like this that is obviously once you've created an account so what we're gonna do is go ahead and click build my first cluster and now we're gonna select some options for our free cluster so you get one free cluster with MongoDB in the cloud at least and you can also host this locally on your own machine but that's a whole different process that I'm not going to talk about today so I'm just gonna go ahead and stick with the default here so Amazon Web Services North Virginia obviously you're gonna want to pick the free tier that is closest to you so you get the lowest latency to your database but I haven't had any issues with speed or anything like that now obviously if you want to you can update some of this stuff and you will have to pay for it on a monthly basis or an hourly basis or something like that but I'm just gonna go through the free tier in North Virginia and click create cluster now creating your cluster does take a few minutes because it actually has to set up and allocate some space on a physical machine for you so give this a few minutes should take about 710 that's what it says here and once that's done we'll get started working with the actual cluster alright so now that our cluster is created we're gonna go ahead and set up a sample database to start and then choose some connection options so we're gonna click right on cluster 0 right here we're gonna go to collections and then we're gonna create a new database so to do this right click add my own data and now type in a database name and a collection name so databases are like specific to your application at least that's the way that I typically do it so let's say we have an application maybe my disc or bought for example I would name this discord and then inside of collections that's going to be the specific information associated with that database so these are like kind of sub lists of the database so maybe you want to have your information organized by like users messages connections something like that whatever it is then you're gonna have three different collections for that now in our case we're just gonna have one collection and one database and I'm gonna talk more about this in a little bit once we actually get to the code so I'm just going to name both of these tests and click create you don't need to cap the collection although you can if you want so we're gonna do that it's gonna take a second load up and now we actually have a database and a collection so you can see that this right here that's showing is actually our database and then this is the collection and you can create new collections by just clicking add here and then adding a new collection like that so once we've done that we're gonna go back to project 0 and we're gonna go to this connect option under cluster 0 now this is pretty straightforward and it kind of walks you through exactly what you need to do the first step is to whitelist your IP so there should be a little button here that says whitelist IP just click that if you're gonna be working on a different computer again need to whitelist that IP as well this is just the allowed IP addresses to access the database then we're gonna create a MongoDB user so that we can login so in this case I've already done one but just fill in the information user name and password next choose a connection option we're gonna go to connect your application we're gonna change this driver to Python because that's we're gonna be using select the option or version you're gonna be using this case 3.6 or later for myself and we're gonna click on copy now once we've done that we're all done everything we need to do from the web browser side and it's time to actually run into our Python code so I'm just paste that connection ID that I had right here in my Python code and now it's time to install the Python module pymongo so to do this we're gonna go to our command prompt and simply type pip install pymongo now this will take a second and I'm assuming you guys already have pip installed on your machine it comes default with Python but if for some reason your pip does not work leave a comment down below or click on the card that I'm gonna leave in the top right hand corner here it'll show you how to fix this command so if you type pip and you get something like pip is not a recognized command click that video it'll show you how to fix this alright so now that we have pymongo installed as our python package it's time to start working with it so the first thing we're going to do is import pymongo from the top of our code here and we're gonna say from PI import client now what we're gonna do is setup our initial connection to what that cluster is so that cluster is the first thing we created and what we're gonna do is we're gonna say in this case I guess we just did cluster is equal to client and then in here we're gonna put this URL that we had inside of quotation marks so we'll put that in here and we just need to modify this slightly so where it says password in here where you're actually gonna just type in your password now my password was just 1 2 3 4 so I'm gonna put that there and for any of you that think you're just gonna go and mess with my database I'm obviously deleting this before I've released the tutorial so go ahead but you won't be able to now after we do that we're going to define what database we're working with and what collection we're working with so this is the connection to that actual cluster so the machine that's holding our database but now we have to pick what database we want so I'm gonna say DB standing for database is equal to cluster and then just like you would index something in a dictionary we're gonna put the name of our database which was test inside of quotation marks inside of brackets like this now we need to choose our collection that we're going to be working with so now we've gone from client to database now collection so we're going to say collection is equal to in this case DB and we name that one test as well so we'll put test right there and now we have access to our collection and we can actually start adding some into our database so the first thing I'm gonna show you is how to add something so to do this what we're gonna do is take collection dot insert underscore one like that and we're gonna put in here what's known as a post now it's time to talk about what a post is so the way that things work in MongoDB is we have starting with our cluster which is kind of that space in the cloud then we have a database and a set of collections now a collection is kind of like a mini database that's the way you can think of it and each collection contains what's known as posts so you can think of them just as entries but we call them posts now posts have a few properties associated with them they look like a Python dictionary when we're dealing withwith them in Python code or like a JSON file or something like that and they always have what's known as this underscore ID tag now this ID is how you're actually going to access all of these posts so you want to make this something meaningful now when we start creating a post what we essentially do is just create a bunch of dictionary keys that map to values so in this case I have underscore ID this is going to be a property of my post or an entry or some kind of information associated with that post and in this case I'm just gonna set like ID 0 ok now maybe we want to have some more information in this post and we're gonna make me call this name so I'm gonna do name and then I'll set this value equal to 10 maybe I want to have the date or maybe I want to have some kind of number associated with Tim maybe I'll do like score and this is going to be equal to I don't know let's say 5 so this is how you create information and as a post in MongoDB so again we go from database to collection to post and then inside a post we have these fields we always have this underscore ID field and if we decline to give it a value it'll automatically generate an ID for us or some arbitrarily arbitrary number and then we can give some values here like name is Tim and score is 5 so now that we've created this post I'm just gonna call this one post like that what we do is we simply insert it by just doing collection don't insert underscore 1 post now if I actually run this code and we look at our database you will see that I insert a post that looks like so let's go ahead and do this control B to run our code no output obviously because this is just gonna work now if I go and I have a look at my cluster and I go inside of collections and we go to test you can see that we have a post here and this is ID 0 named Tim score 5 now these are also called documents I kind of just call them post because that's what they do in the tutorial but they're known as documents or posts whatever one you want to call them and they have fields associated with them so ID name score now I want to quickly show you what happens if I don't add an ID so let's say ID now insert something so named Tim score 5 and I run this code now if I go and look at my database what you can see if i refresh this is give it a second here that we just completely create some random ID which is known as an object ID and obviously this does not mean much to us so I recommend that you always create some kind of ID when you pass in a post or a document or whatever you want to call it into your database but again you don't have to one will be created for you if you do not alright so now let's talk about some more things that we can do with our database alright so another thing we might want to do is insert more than one post so to insert multiple posts and actually as many posts as you'd like what we can use is the insert many methods we're gonna say collection dot insert underscore many and in here we're gonna put a list of the post we want to insert so really easy straight straightforward similar to before where a new post one and post two which are two posts I've just created here again we're just gonna make sure they have an underscore ID if you don't give it an underscore ID it's gonna generate that gibberish that we saw before so I'm gonna run this we're gonna wait a second and have a look at our database and see what we got so we gave it a second and now you can see that we have ID five Joe ID six Bill alright pretty straightforward that's how insert many works next thing we're going to talk about is find so something you're probably gonna want to do with your database is find information now to do this we're gonna use what's called a query now query sounds all fancy and complicated but it's actually really easy and the way that this is set up in MongoDB just makes it super simple so all you do is you put these dictionary things here like this and now you're simply just gonna type the attribute or the field you want to search by and you can search by more than one field so in this case maybe I want to look for a post that has the name Bill well what I do is I put a name here and then I put the value that I want to look for so in this case we'll do name and we'll do Bill and what I can do here is say like results equals collection defined and then I can print out results like that now if we give this a second we'll run the program and we'll see what we get by running that so we get this MongoDB cursor object so obviously that's not very useful to us we don't really know what that means so if we want to actually look through all these results what we need to do is loop through them so we're gonna say four results in results and then we'll just simply print result like that and now let's see what we get so if we do that now you can see that we get this dictionary ID six named Bill and that is the result that we found now say you just want to access an individual element here so maybe you just want the name or maybe you just want the ID of this result well you just do it like you access a dictionary you put your bracket so you put some quotation marks and then you put the field so in this case maybe you want to access what the ID of our result was so we'll just put ID run this and now you can see that it prints out six now if we had more than one attribute or document or post or whatever you want to call in our database that have the name bill that would return all of the results that meet this criteria essentially so this would give us more than one result to printing out now if we want to search by ID what will end up happening is we'll just be finding one document so in this case we do underscore ID and then here we put whatever our ideas so maybe our ID is five again loop through this and I don't know let's just print it out and see what we get well obviously we get the ID of five all right so that's kind of how that works for searching now if you want to do more advanced searching and finding you can actually use regular expressions so I could do something like name and then I'd actually don't really use regular expressions at all so I don't really know how to use them but you can just put a regular expression inside of quotation marks and it will search for anything that meets that criteria now if you want to search for what do you call it like different things as well like you want to search for name and ID and maybe a date or you want to search by multiple fields all you do is put a comma and then put the other field you want to search by and it will return those results so you guys can mess with this and figure out how it works pretty straightforward and pretty simple now this gave us a cursor object which meant that we had to loop through all of our results but sometimes we just want to find one result so if we just want to find one result and we're certain that that's in the database or whatever we can just do fine underscore one and then what we can do in here is the exact same thing as before so we can just query by whatever we want and try to find one result now the important thing is here that we you're probably going to want to search by ID because if you find more than one result I believe this returns the first one but it may actually run into an error so obviously if you're doing find one you're gonna want to make sure that you're only gonna find one so let's go by ID and maybe in this case I go by ID zero and see what we get as that results so let's do this I'm sorry I'm gonna have to print results and here otherwise you're not gonna see anything and give this a second and now you can see I don't have to loop through it to see the results I can just simply have a look at this alright so next thing after find and find one I actually started before I move on I want to show you guys what happens if you find and you don't put any criteria in here so if I just do find and I do that we'll have to go back to this looping example so we'll just say for I don't know X and results print X what happens if you just put brackets like this obviously this means you have nothing you're searching for but what it ends up doing is returning everything because everything fits this format of just being a dictionary so it's gonna return everything so if I do this and I don't know why I'm going to command prompt but I can just run it like this you can see that we print all of the results in our database and we get all four objects or documents or whatever is there so that's how you find that's how you find one and now I'm gonna show you how we can delete and then finally replace so to delete something is really simple as well all we're gonna do is just go in this case results equals collection dot delete and if we do delete underscore one and then in here maybe we'll do like hi underscore ID : 0 well again it's just gonna find the one document that fits this criteria and delete it so let's go ahead and run this give it a second now if we go into our database and we have a look here we can see that ID 0 has been deleted from the database so that's as easy as it is to delete more than one document we can do delete underscore many and then we can fill whatever criteria in here we want so maybe we want to delete all the IDS that have 5 or maybe we want to delete any name that has bill or maybe we just want to delete everything by putting this in here well if we run that then it will delete everything from our database so now if I run this can I have a look here and everything is gone so a powerful command don't abuse it but that is how that works now I just realize I'm gonna have to add some stuff back in here because well we need to do some more tests so let's would he call it insert underscore many and just insert post one and post two here just so we can actually do some more tests and make sure that this is working so let's run that and now what we're gonna do is update and replace so sometimes you want to update entries in your database you don't want to just delete them and re-add them or have to do any stuff like that so what we can do to do that is update so in here we can say update and now again we need to update many or update one in this case we're just going to do update one and what we'll do is we'll do a search query for what is the document we want to update and then we're gonna add what's known as updates what does it update operators or something like that it will tell us what we need to update on so I'll show you how that works in just a second so if I put underscore ID here and let's say we update what IDs do we have ID 5 now what we're gonna do is we're gonna put an update operator which is I'll talk about them in this second here so something like set alright and then we're gonna put a colon and the value we want to set so I'll show you how this works but looking up the documentation here so essentially what we do is we have something like increment or min or max or mol rename these are the different update operators for fields there's some update operators for arrays some update operators for modifiers bitwise I'll leave a link to the description down below but these essentially do what they say they do so set will set the value of the field in the document which we will define set on insert will do what this says unset will remove a field max will go to the max value of that field increment will increment it by a certain amount so if I show you guys how this works here essentially what we do is we go set and then we put : we put whatever the values we want to update so in this case maybe we want to update let's say name and we want to change that to be Tim well that's all we do we do set name Tim now if I run this here like that and we go into our database you can see that we've actually changed those things that were added so now we have an entry that says Tim so there we go ID 5 now says Tim instead of saying whatever the other one did now if we want to add a new field here all we do is just write the field in so let's say like hello and maybe that's equal to 5 and we let that run well now we'll add a new field called hello and that will be a part of this document and give that a second and now you can see that we have hello and 5 so that's as easy as it is to update fields really straightforward if you want to update more than one you can do update many and then same thing here you can just change this to be what do you call it like something that will access more than one document and then this will stay the same whatever you want to set or change or update or increment or whatever that is will happen there so let's say I actually want to increment hello I'll just show you how this works quickly so I go increment and then I go hello instead of now just setting it to 5 what's actually gonna happen is we're gonna add 5 because increment means increment by whatever this value is so if I run this by hitting ctrl B we go back here we refresh now we can see that we've actually updated hello to 10 by using that increment set operator or update operator sorry and you can see all these are here again link in the description all right so the final things that we might want to do is actually count the amount of documents that meet a certain criteria so what we're gonna want to do to do that is you use count underscore documents so I can say post underscore count equals collection dot count underscore documents like that and then all I do in here is put some kind of query so what documents I want and if I print out post count it'll give me an integer value for all of the posts or documents that fit that criteria so in this case I'm searching for everything so it should just give us however many documents we have in there so let's go ahead and run this and you can see that we get two because again we only have two documents that are in our database so that is kind of as easy as it is to it you can see that everything is pretty intuitive we have count documents we have insert many insert one replace all that kind of stuff anyways I hope you guys got some value out of this video and now you're able to use PI and MongoDB again a really awesome resource really easy to use for your own personal projects you want to host some stuff in the cloud you guys have any questions don't hesitate to leave a comment down below we're joined by discord server message me on twitter message me on instagram i'm always helping out people in that way and finally if you guys did enjoy the video and got some value please make sure you hit that like button and subscribe to the channel for more content
Original Description
MongoDB is a simple and easy to use database. This tutorial will show you how to use MongoDB with python and the python module pymongo. Pymongo is the offical MongoDB API that allows for you to easily perfor mdatabse operations.
Please Note: This is simply the basics of MongoDB in python, it is meant to get you started and give you an introduction. MongoDB is capable of much more and I encourage you to read more information from tehe links listed below.
MongoDB Website: https://www.mongodb.com/
PyMongo Docs: https://api.mongodb.com/python/current/api/pymongo/collection.html
PyMongo Update Operators: https://docs.mongodb.com/manual/reference/operator/update/
◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python
https://tech-with-tim.teachable.com/p...
📸 Instagram: https://www.instagram.com/tech_with_tim
🌎 Website https://techwithtim.net
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/pr2k55t
📝 LinkedIn: https://www.linkedin.com/in/tim-rusci...
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
💵 One-Time Donations: https://www.paypal.com/donate/?token=...
💰 Patreon: https://www.patreon.com/techwithtim
◾◾◾◾◾◾
⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡
Tags:
- Pymongo Tutorial
- Python MongoDB Tutorial
- MongoDB with Python
- Pymongo MongoDB
- MongoDB Tutorial
- MongoDB
- How to use MongoDB
- How to use Pymongo
#mongodb #python #pymongo
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Common Next.js Errors (and How I Solved Them)
Dev.to · gary killen
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI