MongoDB Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you need a database for your project then mongodb is a great choice but it can be difficult to learn at first so in this video i'm going to break down every important concept of mongodb so you can become an expert by the end of the video and we're going to cover everything from the basics of create read update delete all the way to more advanced queries and advanced updates so make sure you stick around to the end of the video [Music] welcome back to web dev simplified my name is kyle and my job is to simplify the web for you so you can start building your dream project sooner and in this video we're going to be doing a mongodb crash course now to follow along in this video you're first going to need to install mongodb i'm going to have links to these pages in the description below but essentially go to this page scroll down find your operating system and then just install the community edition of mongodb walk through the install step and that'll be all you need to do and then once you have mongodb installed you're gonna need to install sh this is the shell that you can work with to actually access and read database queries so just come down here to your operating system click on it and then follow the steps down below it's pretty straightforward and once you have that done you can open up a terminal which i just have here and you can type in the command sh that's going to open up that shell and as you can see it says we're using mongodb version 5. we have version 1 of sh that's because this version 5 of mongodb and sh are very new they came out just a few weeks ago so this is going to be the most up-to-date information you can find now the first set of commands i want to talk about are just basic commands that allow you to actually use the sh terminal and be able to create and view different databases and first you'll see that we have the word test written here that's essentially the name of the database that we're currently using this is just the default database you get started in it's a test database it has no data and it doesn't really exist yet until you create data in order to view your databases you need to type in show space dbs and dbs just stands for databases and you'll see here we have an admin an appdb a config and a local when you type this in you should see just admin config and local and you won't see this appdb that's because i created this appdb already and it has some data already in it now if you want to be able to access and use a database just type in use followed by the name of the database you want to use so in our example we could say appdb to use the appdb database or we could create a brand new name right here if we wanted to and that's going to create a new database and put us inside of that so for our case let's just jump into this appdb database now since that exists i click use you can see it says switch to that database and now it says appdb instead of test now in order to view what collections we have in that database we can just type in show collections and in mongodb there's essentially two different things you have databases which are just like databases and sql and then you have collections and collections are like tables inside of a database if you're familiar with sql it's just where you store your data and you can call them whatever you want so we have a user's collection inside of this database now obviously we don't really want to use this database because it's already pre-built so i'm going to show you how you can actually delete an entire database and to do that you just type in db dot drop database and call it like it's a function the nice thing about mongodb is if you're familiar with javascript a lot of the syntax in mongodb is very similar to javascript you're calling functions and passing them parameters so if we type this you can see it says ok one and it dropped appdb and now if we click show dbs here and type that in you can see that that appdb no longer exists and also if we want to clear our screen we can type in cls it's going to clear our screen completely and put us back at the top this is just great if you have a bunch of junk on your screen one thing you'll notice though is we're still in appdb and that's because monkodb is kind of interesting compared to other things like sql and that's that you don't actually need to ever create a table you don't need to create database users you don't need to create collections that's because whenever you try to access something if it doesn't exist yet and you try to put data in it it'll just automatically create itself so if we try to add data to this app db that we're currently inside of it'll now recreate that database because as you can see if we do show dbs we have no databases but now we can actually try to add something to this database and it'll recreate that database now the last command i want to show you before we get into creating data is just the exit command you type that in and it exits you out of the terminal and we can come back in here with sh and re-enter that terminal so that's just one really simple command that you can use to exit out of the terminal so now that we're in the terminal i just want to type in use appdb and it's going to use that app database you can call this whatever you want it really does not matter and that's just because if we type in showdbs again you'll see this database doesn't exist yet we're just going to be using it and then adding data to it which will automatically create that database for us now to access the current database you're inside of you just type db that gives you the current database and this has a bunch of functions essentially on it so we can say db dot and we can say the name of a collection so a collection like i said is a table so we can call this whatever we want we're going to call ours users where we're going to store all of our user data and then to insert into this table you just type in dot insert one and this allows us to insert one single record inside of this user's collection and this insert one is a function and inside this function all you do is you just want to pass it an object and this object is just a javascript object essentially it's like json formatted so what we want to do for our object let's just pass in a name here and the name is going to be john and we hit enter and you can see it says true and it inserted that with this id right here so now if we wanted to be able to see this information we can just say db.users to access the collection and we can call the find method the find method just gets every single thing inside of the database and as you can see we have an array with one object that object has a unique id that's automatically generated and then it has the name of john now this underscore id is something that's automatically generated by mongodb every time you insert a record so that's really nice you don't have to worry about generating unique ids it just does it for you also something interesting about mongodb that you may have noticed is we didn't have to define any columns we didn't define any schema we just said insert this json object that has a name and the name is john there's no schemas there's no columns that you have to worry about in mongodb and instead you worry about things called documents essentially every single object you store inside of a database in mongodb is called a document and documents live in collections and collections live inside of databases so we have our appdb which is our database users is our collection and this john document right here that is our document inside of our users collection and because there is no schema it means you can actually add anything you want inside of your collections and they don't all have to have the same columns or the same fields so we could come in here and we could say db.users insert one oops insert one and we want to insert a brand new user and this user is going to have a name and we're going to give them the name of sally so so far this is exactly the same as john it has just the name field but we're going to add additional information for example we're going to put an age in here and we're going to say that they are 19 as well and then we're going to add in more information we're going to add an address and one really nice thing about mongodb is you can actually nest things inside of it because just like json you can put objects inside of objects so we're going to nest an address object inside of our user right here so we're going to say the address has a street and this street is just going to be 987 north street and then we're going to close off that object and we're also going to put an array of hobbies inside here so let's put an array of hobbies and we're just going to put one hobby inside of here which is running and if we just close this off and run this you can see acknowledge true and inserted that user and if we do db.users.find you can now see we have john up here is our first object and then we have sally down here which has the name sally age 19 the address which has the street inside of it and then our array of hobbies so with mongodb we don't have to have all of the documents inside of our collection share the same fields and also we can nest things such as nesting an address inside a user or nesting an array of hobbies inside of here which is something that's pretty much impossible or very difficult to do in sql let me just clear my screen real quick and get back to the top and i want to talk about how you can insert multiple records at once so if we go db.users there's another function called insert many and this is just like insert one but it takes an array of objects instead so we could say that we're going to have a name here of jill and then we're also going to just close that off and pass in another person which has a name of mike so now essentially we're passing an array to this insert menu which has one object here of jill and one object of mic we hit enter you can see it inserted both of those if we do dbusers.find you can see that we now have jill and mike at the bottom now in order to populate our database with a little bit more information i'm just going to do another db.users.insertmany i'm actually just going to copy over a bit of information just a few different objects so that way we don't have to manually type these in and essentially i'm just inserting a user here that is myself kyle and then we have another billy down here so if i hit enter and just do db or first i'll clear and then do db.users.find oops and as you can see if we type in an error it's really nice they have really good error handling so it tells you hey you just messed up your error right there so now you can see we inserted this user kyle which has age 26 hobbies of weightlifting and bowling and then we added an address same thing here billy has swimming bowling age 41 and another address this is just so when we do more complex queries we can actually query in all these different pieces of information for our database now speaking of querying that's the very next thing i want to do how do you read information from your database well we figured out that users has a find method and this find method just gets every single thing from the database as we can see but what we can do is actually additional information on top of that so we could say db.users.find and then we could call additional message after this for example if we wanted to sort things or we wanted to limit so we could say you know what limit me to only two results and now you can see we're getting the first two results in the database but what if we wanted to sort them by for example names so we wanted to get them in alphabetical order well we could come in here we could say db.users dot find and then we can just say sort and for sort this takes an object and this object has a key which is the thing you want to sort by so for example we want to sort by name and then you pass it either one or you pass it negative one depending on the order that you want to sort in so let's just pass in one at first and then we're going to limit this as well to 2 and as we hit enter you can see we get billy first and then jill and that's going in alphabetical order now if we pass in negative 1 it's going to do the reverse order so now you can see we get sally first and then mike next and that's because it's going in reverse alphabetical order and a really nice thing that we can do is we can actually sort by multiple fields at once so if we just come in here we wanted to sort by name and then we wanted to sort by age for example so let's do age first and we're going to do that in order and then name second in reverse order now you can see that we're getting mike and john showing up here and that's just because they don't have an age so they're showing up at the very beginning we change our age here to negative one you can see that now it's sorting in age reverse order first so it's going 41 and then 26 and if we had two people with the same age then it would also sort them by name in reverse order as well and the last thing i want to talk about of these sorting type methods is skip and skip just allows you to skip the first few things so let's say db.users.find and we're going to limit this to just two you can see we get john and then sally but if we put in a little skip here let's just say we want to skip one that's going to skip the first entry so now we start with sally and then we also get jill as well which comes after sally now these message are great but really what we want to do is build a query on different fields so we want to do essentially where queries if you're familiar with sql to do that we just pass a parameter to our find method so we're going to say db.users.find and here we just pass an object to find and this can get really complex or be pretty simple but the main thing you have to worry about is you want to pass in the thing that you want to find by let's just do name and then you can pass it a value so let's just say kyle what this query does is says hey find everyone where the name is equal to kyle and as you can see we get just one object where the name is equal to kyle that makes sense we can also query by additional things you know we could come in here we could say age and we wanted the age to be for example 26 and now you can see we get kyle again because he has the age of 26 but it's important to note this age is a number so if i tried to query where the age is a string of 26 we get nothing back and that's because the age is a number again so it's only going to match where the age is equal to this as a number not as a string now also if you're familiar with sql you're probably familiar with the select syntax where you can select just a single row or a single column from the database so i want to get just the name or just the age or the name and the age but not the hobbies well we can actually do that as well we can say db.users.find and we're just going to find where the name here is equal to kyle and then what we need to do is pass a second parameter which again is an object to find and this object here tells us what parameters we want to get so let's say we want to get the name field we're just going to pass a 1 to that we pass a 1 here that's saying hey we want to get this field so let's get the name and the age and that's the only thing is going to be returned as you can see we get name of kyle age 26 but the hobbies in the address are not returned you also notice the id is returned automatically if you don't want the id be returned just put underscore id and then set this to zero that's saying hey don't get me the id field now as you can see we get just the name and age without the id also if we wanted to come in here and we said hey you know i want to get all the fields except for the age i don't care about the age if we just put age zero here and nothing else it's going to say hey get all the fields but don't get age so as you can see we get name hobbies address and id but the age field is left out so you can either use ones to say you want to select only these fields or you can use a 0 and say hey select all the fields except for this and this is a pretty straightforward where clause we're saying where name equals this but what if we wanted to do something a bit more complex well that's where we can do complex queries instead so let's say db.users.find and again we're going to pass this in object and inside this object we're going to say hey we want to query on the name field but in order to do a complex query what we need to do is put again another set of an object and this object is going to contain all the information for the complex query and inside of mongodb pretty much every single complex query is going to start with a dollar sign and then you're going to denote what you want to do so if we want to check for equality just like we were doing before you could say dollar sign eq and then we can pass in here that we want the value to be sally and what this is saying is hey get me where the name is equal to sally just like we were doing before and as you can see we get sally returned but we could also do something else for example we could do not equal and this is going to get us everything that is not equal to sally so now if we hit enter and i make sure to change this to n e instead of n eq you're gonna see we get all of the users returned except for the ones where the name is equal to sally now there's a bunch of these different types of complex queries so i want to cover as many of them as i can as quickly as possible so we'll say db.users.find and we're going to find here on the age property because that allows us to do a lot of these and again we want to make sure that we have an object and this object we're going to do greater than so dollar sign greater than and we're going to say everything that has an age greater than 13 and as you can see we get only the users that have an age greater than 13. we could also do greater than or equal to which is gte so if we did like 19 for example you can see greater than or equal to 19 returns 19. we can do less than or equal to as you can see only sally's return because she's the only one with an h less than or equal to 19. and if we do less than we get nothing returned because nothing has an age less than 19. now also some more queries that we can do we just come in here db.users.find and let's say that we want a query on name this time and we want to say where the name is equal to one of two values we can use dollar sign in and that essentially says hey if the name is in this list of names then return it so we can pass it an array and we can say kyle and we can say sally and this just says hey is the name kyle or is it sally if so return it and as you can see we get kyle and we get sally being returned we can also do essentially the opposite of n by saying not in by just putting an n in front of this now it gets us all the users that don't have a name that's either kyle or selly and as you can see kyle and sally are the only ones left out of this list another query that's really important that you're probably going to want to do we'll just say db.users.find we want to find by age and we want to check to see hey which ones have an age that exists because as you know some of our documents don't actually have an h so to check for exist we just do dollar sign exist and we say true and this is going to return only the objects that have an age on them as you can see all three of these objects have an age if we pass false to exist now it's only going to return to us the objects that don't have an age so we can check whether or not there is an age or there isn't an age and this is going to return only things that don't include the key one thing to note though is this is only going to check to see if the key exists if we have an object for example a name here of john and the age is set to null exist true is going to return the age that has null because it only checks to see if the key actually exists on the user and not if the value exists i can really easily show that by just saying db.users.insert1 and we're going to insert something that has an age that is set to null and a name which is just going to be bill and if we just close that off and i do that check where exist is true you can see even though the age is null it still returns when we check for exist true up here now the next couple queries i want to talk about are going to be pretty complex queries when you want to combine together multiple queries so say db.users.find we're going to do a find here on age and again we're going to wrap this inside of an object in this object we want to check to see when the age is going to be less than a certain value and greater than a certain value so we can say gte so we want to check is it greater than or equal to the value of 20 and we want to say hey which check also less than or equal to of 40. we hit enter and you can see we get all the users that have an age greater than 20 and less than or equal to 40. and this way this works is if you pass an object to a field essentially it does all of these as an and query so it's going to say okay where the age is greater than or equal to 20 and it is less than or equal to 40 do this query if we combine additional things on this for example we pass a name here we say the name must equal sally now it's saying that the age has to be greater than or equal to 20 less than or equal to 40 and the name has to be sally we change this to kyle we'll see that we actually get one result because this name is equal to kyle and the age falls within this range specified so this is one way where you can do an and query another way that you can do an and query though if we just clear our screen here we say db.users dot find and inside this find we're going to pass dollar sign and and when we use this dollar signed and essentially what we're saying is hey pretend that we're doing another find and inside of here we're just combining all the things inside the and so and takes an array and in this array we can just do what we did before in a fine so we can say you know we'll do age which is equal to exactly 26 and we'll come in here and we're going to say name oops make sure this is an object is going to be equal to kyle make sure i close this off so now you can see we passed in dollar sign and which takes an array and this array has an age of 26 and a name kyle being passed in so it's combining both of these together and as you can see we get kyle being returned generally you don't really need to use this dollar sign and very much and that's just because you already can do ands almost every other way by just putting them all inside of one query but one thing that you do need is ors if you want to do an or you're going to need to use the dollar sign or syntax so let's say the age here is going to be whoops less than or equal to 26 or the name is going to be kyle and actually let's make this less than or equal to 20. there we go so now we're saying hey i want to see where the age is less than or equal to 20 or the name is kyle and of course we're getting an error that's just because i need to make sure here i close off this object now you can see we're getting sally returned because her age is less than 20 and we're getting kyle returned because the name is equal to kyle so the or syntax is really important because there's no other way to do or unless you use the dollar sign or here another thing you can really easily do if we just say db.users.find is if you want to do a not query so let's say that we want to check here when the age is going to be less than or equal to 20 and let's say we actually want it to be not less than or equal to 20. well what you can do is you can just create another object and put the not inside of it and you just pass an object to not and essentially what not does is it just negates everything that's inside of it so now we hit enter and i make sure that i add in the correct number of curly brackets you can see we're getting all of the users where the age is not less than or equal to 20. so we're getting all the users where the age is greater than 20 but we're also getting all the users where there is no age defined as you can see here these users have no age and this one has an age of null while if we for example did something like greater than 20 which is essentially the same thing but we remove this knot and hit enter and i make sure that i have the object here you can see now we're only getting users that have an age defined so that's one thing that knot can kind of do differently so if you do a knot it'll actually return all the things that don't have ages in this case really for the most part though you don't ever really need to use not because less than greater than not equal to equal they already all have the not version but it's good to know that it exists now the last complex one of these queries i need to talk about requires me to add a little bit additional data to our database so we'll say db.users.insert many inside of here i'm just going to be inserting users that have a balance object as well as a debt object so if we hit enter you can see we inserted these two users and what i want to do is i want to find the users where their debt is greater than their balance essentially they're in debt so to do that we can say db.users dot find and in order to compare two different properties on an object what we need to do is we need to use the dollar sign expr that stands for expression and inside of here you can run a query that compares two different things so let's say that we want to do a greater than query we want to see where the debt is greater than the balance so we can say greater than and inside of here we're going to pass it in array and this is going to say hey where the debt column is greater than the balance column so the first thing we passed this array is going to be the first column we want to check to see if it's greater than the second column we hit enter and i make sure that i close everything off you'll see now we're actually getting all of our results being returned and you're probably wondering why is this the reason is is because if you want to actually access a column instead of a specific value you need to put a dollar sign in front so now you can see we have dollar signed debt and dollar signed balance now when i hit enter i only get the object being returned that has a debt of 200 and a balance of 100 so the debt is greater than the balance so if you're going to be using columns just make sure you use that dollar sign syntax in order to do that now if we just clear this out i want to talk about a few additional find methods that are just going to be continuations on what we've already talked about so we can say db.users.find and if we want to find based on a nested field so example we have address.street that we want to query on well in order to make this query we just wrap this address dot street name in quotes and we have an address object and inside it has a street property so you can say address dot street just like you're going to be accessing this with javascript and then we can use all the additional queries we talked about so we get set to a specific value for example main street like this i close that off you can see we get the user that has 123 main street as their street or you know we could combine this with you know less than equal to is greater than we could do all that additional stuff passing it in just like this this address dot street is just like any other column name also something that we can do is we can do a find one so that allows us to just find one object that matches the query so let's just do here a find one and we're going to pass it in age and we're going to say where the age here is less than or equal to 40. for example close this off now you can see it just found the first user that has an age less than or equal to 40. so if you did some kind of sorting for example you could find the first user that has an h less than or equal to 40. that's what find one is going to do and then also we have the ability to count so instead of find one we could change this to count documents now we hit enter and you can see there are two people in our database that have an age less than or equal to 40. now that right there pretty much covers everything you need to know about querying data so next we want to talk about updating data which is of course an incredibly important part of any database so in order to update a field we just want to say db.users and we have a method called update1 and this allows us to update to just the very first thing just like find one this is going to be the same thing but for updating one object and the first parameter to update one is just going to be the thing we want to filter on this is the exact same thing that you passed to find all the things we talked about inside a dot find work for this update one so let's say we want to get the user where the age is 26 this is the user with the name of kyle so where the name is 26 we want to update them and then we can pass in here all the update things we want to do and this is kind of confusing for most people because you would think okay we'll just say you know age is going to be 27 and that's going to work right but this is actually going to throw an error as you can see update document requires atomic operators and that's because they require dollar sign operators so in order to do a set operation where you want to set a field for example we want to update our age to 27 you need to use dollar sign set and then pass it the things you want to update so in our case we want to update our age to 27. hit enter you can see it updated and then if we do a db.users.find one and we're just going to do where the age is now 27 you can see we got that user kyle which has an age of 27. and up here if usually if you're going to be doing an update you're going to want to update based on the id so we could say where the id is equal to and we'll just copy this id right here and paste that in and now we can set for example the name to new name hit enter and we're going to do a find based on that id as well so we'll say underscore id is equal to the id we have here now you can see the name has been updated to new name now there's a few other things you can do for updating so we'll say db.users.update 1 we're going to pass in that underscore id again because we want to get just that one user and instead of doing a set here i'm going to do an increment so i can actually increment the value of a number so i want to increment our age property by let's say 3. so now we're going to go from 27 to 30. and if i just close this off hit enter and i find that user so we can say db users find one you can see now their age is 30 has incremented it by 3. another really cool thing that we can do with update 1 instead of increment is we can rename a column so i want to rename let's say the name column i want to rename it to first name just like that if i make sure here that i spell rename properly hit enter and now i find that user you can see that their name property has been renamed to first name another thing we can do is unset a property so if we just say we're going to do dbusers.update1 and instead of rename here we're going to do unset and i want to unset the property here which is going to be age so we're just going to say age in order to make sure we do this properly we actually need to pass this in as an object and just set the value to anything it really doesn't matter we'll just do an empty string so now we're unsetting the age property we're just removing it completely so if i just make sure i have enough closing parenthesis here and i find that user you can now see we completely removed the age property it didn't set it to null it completely removed it from the object another thing that we can do is actually add two arrays so if we just come back to this update here we can say that we want to push a value into our array and inside of here we just want to say our array is going to be called hobbies because that's the array right here we want to push to this hobby's array and we want to push in the value of let's say swimming for example we just hit all those parentheses make sure they're closed off hit enter now if we find that user you can see we added swimming to the end of the array we can also do essentially the opposite of removing so if we just get that update again instead of push we can say pull and let's say that we wanted to pull from here something like bowling so we'll just type in bowling hit enter and now if we find that user you can see we removed boeing from the list and the interesting thing about this push and this poll is that it takes the same queries as find one so if we had like an array of numbers we could say hey remove everything less than or equal to the value of three and it's going to remove all of those so again all of those queries that we talked about in find they work all over the place in tv now the next thing i want to talk about is updating many users at once so we can just say update many and this is going to update everything that matches a specific query so let's say we want to get them where the address exists so we're just going to say dollar sign exists we're going to set that to true so every single thing that has an address is going to be matched by this query and all we want to do is just unset the address so we're going to remove the address from everything i'm just going to say address and just put an empty quote inside of here close this off hit enter you can see we've updated three different things and now if we do a dbi users.find to get everything you'll notice none of our users have an address because we got all the users that had an address and we removed that address field so that's what update menu allows us to do and then finally we have replace which is kind of interesting so let's just say db.users.replace1 and this is how we replace one single object let me just actually clear this out so we'll say db.users.replace1 and replace one takes the same exact filter so we can say age is equal to 30. that's the user that has the name kyle and then here we just pass it a new object so let's just say the name here is going to be equal to john and now if i hit enter on this if i just close this off hit enter you can see it worked and now i can say db.users.find we can say name is equal to john close this off and you'll see we get this user here what happened is we found the user with the age of 30 and then replaced that entire object with whatever we passed in here so since we only passed a name of john that's the only thing that was saved to that user so replace essentially takes everything at that location deletes it and then replaces it with what we pass in to replace generally 99.9 of the time you're going to want to use update instead of replace because you don't want to actually replace an entire object you just want to update a few fields at a time now that's everything that we need to know about updating so let's move on to deleting so we can just say db.users and we can do delete one to delete one user and it takes the same exact thing as find so we could say here name is equal to john to delete that user we just added you can see it deleted one user db.users.find where the name is equal to john and now you're going to see nothing gets returned because we just deleted that user we can also do a delete menu so db.users.delete many and here we're going to delete everything that has no age so we're going to say age we're going to check for exists so we'll say dollar sign exist false this is going to be anything that doesn't have an age is going to be deleted you see it says it deleted 5 different thing and if we say users find you can see now we only have users that have an age even if the age is null it just has an age key on it and that right there is everything you need to know about mongodb if you enjoyed this video i highly recommend you check out the full cheat sheet on mongodb i have linked in the description below and with that said thank you very much for watching and have a good day
Info
Channel: Web Dev Simplified
Views: 93,212
Rating: undefined out of 5
Keywords: webdevsimplified, mongodb crash course, mongodb, mongo db, mongodb tutorial, mongodb terminal, mongosh, mongodb how to install, mongodb tutorial beginner
Id: ofme2o29ngU
Channel Id: undefined
Length: 29min 58sec (1798 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.