MongoDB Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] this video is sponsored by dev Mountain if you're interested in learning web development iOS or UX design dev Mountain is a 12-week design and development boot camp intended to get you a full-time position in the industry to learn more visit dev mountain comm or click the link in the description below hey what's going on guys welcome to my MongoDB crash course so I did a crash course about three years ago on but some things have been updated we now have compass which is a graphical user interface that you can install locally we have a MongoDB Atlas which is a cloud implementation of so some things have changed so I want to touch on that stuff I also want to jump into the shell I'm going to show you how to set up locally whether you're on Windows or Mac I already have it installed but I'll go through the steps for each so that you can get set up and then we're gonna jump into the shell and we'll look at some commands to insert data find data we'll look at some of the different operators we'll look at updating data things like that and then later on we'll jump into MongoDB Atlas which is a cloud implementation of MongoDB which I actually use most of the time and and I recommend most of the time because then you don't have to manage your your MongoDB installation you just have it in the cloud you just easily connect to it through your application alright so let's talk a little bit about what is it's a no SQL database which stands for not only SQL and I'm sure most of you are familiar with relational databases like MySQL and Postgres and Oracle they store tables of data in columns and rows with no SQL databases like MongoDB we store our data and collections of documents okay so documents are very similar to JavaScript objects or JSON objects it's actually beasts on or Beeson which is similar to Jason but it has some added datatypes so if you're familiar with JavaScript then or you know picking up is pretty easy and I believe that's why it's so popular in the JavaScript community you have the myrn stack the mean stack the M and all these stacks is MongoDB as far as advantages it's very very scalable it has built-in replication and sharding and it's it scales horizontally meaning you can just add very cheap machines to build up your cluster it's performant it's fast and what I love about it is how flexible it is so the data structure like I said is very much like just a JavaScript object basically but you don't have to map out your entire data structure beforehand like you do with relational databases so with a relational database like MySQL you have to create all your tables you create all your columns every property in that column you have to decide you have to decide what data type it is if it's a you know a varchar' or if it's an integer there's none of that with MongoDB we just kind of we have the freedom to structure our data however we want through our application and that's that's why I like it so much and I'm not saying it's better than relational databases I think there's there's certain situations certain use cases that are better for you know with MongoDB and certain projects that are better with relational databases all right so I don't want to Yap too much let's jump in and get started now if you're on window obviously I'm on Mac so if you're on Windows I just want to kind of explain to you how to get a local installation set up so you want to go to products this is MongoDB comm you want to go to products MongoDB server download the latest release server right here and then go ahead and download the file I believe it's an MSI file it's just an install or you just go through you can set it so that MongoDB is as runs as a service so it'll run in the background it'll also come with compass which is a graphical tool and if you want to run the shell you simply go to the the MongoDB location which is going to be in your Program Files actually I'll just paste this path on this Green here so you can see it so if you're on Windows I'm going to paste it in the URL bar so Program Files MongoDB server whatever the version is and then the bin and inside this bin folder you'll have a executable so you just want to go into a command prompt and run and that will run your shell alright now if you're on Mac then you you can download we could download this file and move the binaries over to the bin folder but that's kind of a pain an easier way to do it is using homebrew so if you have a Mac you can just grab this copy this command right here from brew dot sh and put that in your terminal and run it and that will install homebrew once homebrew is installed this page right here I'll try to remember to put this in the description but you just need to run basically three commands to get set up this right here brew tap MongoDB slash brew then we install the community MongoDB community version and then you can either run it just right from the terminal or you can run it as a service which is what I would recommend so you run this command right here and then finally just run you can run it from anywhere and it should open up your shell alright and I'll just give you an example of that so if I say I've already done that those won't ran those commands and you can see now I'm in my shell and I can control l2 clear this up all right now before we start to work within the shell I want to show you compass real quick so I'm gonna open compass oh and I should show you how to install it so if you go to products so if you use homebrew it doesn't install compass you just have to go to products MongoDB compass try now and just go ahead and download and install it and that will set it up alright so once you run compass you can just leave all this stuff so localhost the default port for is two seven zero one seven we'll click connect and you're gonna see these three default databases admin config and local and with compass I mean you can it's just a GUI you can create a new database like let's say just one and we can actually create a collection as well a collection is similar to a table it just holds documents of data so for instance if we add maybe products and create database you can see we've created test1 if we go in here we can create other collections we can also go into the products collection we can insert a document so all documents are going to have this underscore ID field in MongoDB which is an object ID this is a unique identifier it's kind of like having a primary key in a relational database so though it's that it'll have that automatically and we could go ahead and create like you know I don't know a name and say like test product we could say insert and we inserted a product alright and I mean you can use compass if you want I think it's good to kind of manage your database a little bit but you really want to get comfortable with the shell and with the commands the different methods to work with documents and that's what we're gonna do so I'm gonna go ahead and close up compass and go back into my shell here and first thing I'm gonna do is say show DB's and by the way I'll have a file a link to a markdown file in the description which will be a cheat sheet with all the different commands that I'm gonna show you and you can keep that for reference for you know future use so if I do show DB's you can see the test one database that we just created if I want to use that I simply say use test one and now I'm in that database and I can say show collections and we have a products collection now I'm gonna actually create a new database from the shell so I wanna I want to delete test one so to drop a database you just simply need to use it and then do DB drop database like that okay so now if I say show DB sweeney a space there you'll see that it's gone now to create a brand new database we simply say use I'll just call this Acme and that created the Acme database and switch to it now I want to show you something if I do show DB's it doesn't show the Acme database and that's because there's nothing in it there's no collections or anything so it doesn't show up so we're already in the Acme database in fact if you want to check which database you're in you simply say DB ok and then let's create a collection so we'll say DB dot create collection and let's create a collection called posts so we get ok one now if I say show collections you can see we have a post collection so now we're ready to start in basically inserting data into our collection to work with or inserting documents so data documents rows records I'm using all these words interchangeably now I'm gonna go ahead and just open up an empty sublime text file just so that I can kind of structure the query you know to make it more readable and then I'll just paste it in so we want to take DB and then the collection which is post and then say insert and then we just simply pass in an object with whatever we want so let's say title I'll just say post one we'll give it a body it'll say body of post one let's give it a category so category will say news we can do a date and in we can just say date like this and it will put in the current date and time all right so let's say I guess we could put some other stuff here so we just have strings we have a date we can put something with a number so let's say like likes maybe each post has a certain amount of likes we'll just say for we could have an array so maybe tags say news events and then we could also have embedded objects so maybe we have a user object associated with this post and maybe have name and maybe status say author so something like that so we can have arrays objects all that stuff so I'm gonna go ahead and just copy this and paste this in here and run it let's see I forgot a comma right here let's try that again paste that in okay so we get this insert and inserted one now if we want to insert more than one row at a time we can do that as well with insert many so I'm actually gonna I'm gonna paste that in real quick I'll put it here first just so you can see the syntax again I'm gonna have the file in the description that has all this stuff but we can do DB dot posts insert many and just pass in an array of posts now notice that these don't have like likes and tags and user and that's absolutely fine unlike relational databases we don't have that strict data model that we have to follow we can put absolutely anything we want in any document which is which is you know I think is really good it's really cool so let's copy this and let's paste this in here and run it and you can see that it gives us all the object IDs now if we want to query the data or find the data let's close that up for a minute we can do DB dot and then the collection so posts and dot find so if I run that it shows us all of our posts now this is kind of this is kind of hard to read so there's actually a method called pretty that we can tack on to our find and now you can see that it's much easier to read and again you see the first post has some some you know like stags it doesn't matter we can put whatever we want so let's see if we want to narrow it down maybe we want only the Neuse categories we could do d b dot post dot find and this is similar to like a where clause in SQL so if we want to find where new I'm sorry category is equal to News and now you'll see we get just the two and we can also tack on pretty here if we want to make it more readable okay so that's how we can kind of narrow things down we can also sort so if I do let's do a find and right here we'll do dot sort so we have a sort method and then here we can put the field we want to sort by let's say title and then we can either put one for ascending or negative one for descending and if I run that you'll see that now we have let's see post fours first so it's in alphabetical order for the title post for post one three and two so that's how we can sort okay if we want to count how many documents we have we could do let's count how many news doc categories so we can just do dot count like that you can see we get two okay so we can use the count method if we want to limit our records we can do that as well let's just let's get rid of this and let's say dot so find limit and we'll limit to two and you can see we'd get just two records back we can chain on to if we wanted to sort and limit we could do that so we could say like swords and sort by title say negative one to do descending so you can see we can just chain things on just like JavaScript let's see we can also use for each so if we want to maybe print something out for each each post we can do that so DB dot tostart or each and I'm sorry it's find dot for each and this actually takes in a function and we can put whatever we want here I'll just say doc and so let's say and here we want to do print will say prints and we'll say blog post : and then we can actually concatenate on doc dot and then whatever field we want let's say title so if we run that we get blog post and then we get each title alright so we could do that let's say we want to find just one specific row we could use fine but we could also use find one so find one and we'll say say category news it should just return the first one alright let's say we want to update so I'm just gonna go back into sublime text for this yeah let's see well do DVD post update now I'm gonna show you the first way of doing this where we actually have to put it's just going to replace the entire thing so if you do it this way you have to be sure that you have you update with all the fields that you want to be you know for that post the first parameter is gonna be the post we want now I'm gonna grab it by its title so we'll just say post let's say post two now usually you don't want to do this you want to use the ID because there could be multiple titles but um are you know the same title but I'm just doing this for simplicity so that's what we want to grab is post two and then the next parameter I'm just gonna go on to the next line here we're gonna specify what we want to row to update it with so let's say title post to let's say body and I'll just say new post to body and then let's do let's actually leave out the category just so I can show you it this will replace everything and we'll just put the date all right now we can pass in options as well as a third parameter and one option we have is up cert and I'm gonna set that to true and what that does is if this isn't found it'll actually create it it'll insert it so that's what the up cert is it's like an update hand and insert so let's copy that and paste that in and run that and then if we do adb dot post dot find and let's find what is a title post - juts dude pretty and you'll see that the category is gone because it basically it just replaced it with this now if if you want to just replace you know one or two fields or whatever but keep keep what's already there then you need to use the set operator so I'm just going to get rid of the up cert and let's let's do this again will update post too but now we're gonna inside these curly braces we're going to use money signs set okay so this is an operator and inside set we're just gonna say category and let's say technology so using set it's just gonna update this or in this case at it because it's not even there but if we wanted to change something else as well like let's say the body will change the body back to was it body of post too and let's copy that and run that missing let's see oh I forgot the khamar alright so let's run that and then let's check it out and now you can see that the body was updated and the category was also added here technology alright so you know for the most part you want to use this set operator when you're updating at least with the standard driver here alright so we also have some other operators like increment so we have the the likes property for post one let's say we want to increment that right now we have let's let's look at post one so post one likes is for let's say we want to increment that by two then we could do DB dot posts dot update and let's say we want to update where the title is post one and then as a second parameter passing our curly braces with the i NC operator another set of curly braces and then what we want to increment which is likes and let's say we want to increment it by two and now if we take a look at post one you can see that likes is now six okay so it's another operator we also have the rename operator so if we want to rename a field let's say we want to rename likes to views so we could do let's see well grab post one and instead of ink we're gonna say rename and let's rename the web section I wanted to leave that likes let's rename that to views and now if we take a look you'll see that likes is now views all right so now let's take a look at deleting so we have what four posts right now let's say we want to delete actually let me do a let's do a find here so find with no parameters will give us all of our posts so let's say we wanted to leat post four now again you would normally do this by the the object ID but just for simplicity I'm going to use the title so let's do DB dot posts dot remove and let's say we want to remove where the title is post four and you see we get right result and removed one and if I do a find again post four is now gone so simple let's see we can also have sub documents now in a relational database let's say we have we have blog posts and we want some comments usually what we would do is have a separate table for comments we'd have to create a foreign key that links to a primary key on the other table well with we can simply embed the comments right inside of the post collection and that's just one way to do it of course you can have a separate collection for comments if you want but this makes it easier in a lot of cases so I'm gonna go into sublime here we'll keep the update let's update post one and we're gonna keep set here but I'm gonna add comments and let's set that to an array of objects and we'll have let's do user say I don't know Mary Williams and I don't know it's do comment body I just say comment one and we can even have the date and I'll just copy this we'll put one more say Harry White and comment - okay so we can go ahead and grab this and paste that in and run it and now if we do a find of post one you can see now we have the comments embedded right in the right into the right in the post okay so when we query this when we're within our application we can simply do you know post dot comments or whatever and we can get the comments and there's no complicated joins and stuff like that let's say we wanted to find a post by maybe we want all the posts that have the comment with this with Mary Williams we can use a an operator called LM match so to do that let me just do it over here so we would do DB dot post dot find and inside here let's say comments so we're looking in the the array of comments and we can do money sign L a match and let's say we want to match where the user I think I used Ted user I'll say where the user is equal to Mary Williams so if I take that and again this will be in the in the description and it will give us the posts with Mary Williams we could add dot pretty the to that and there we go we just get post one all right so let's take a look at search if we want to do a text search we can actually add indexes so to add an index we can do d b dot post dot create index and let's say we want to add a text index to the title we want to search by the title so we'll say title text okay so now we have an index on the title field it says num num indexes before which I believe is the ID and then after we have to because now title is and has an index so to search we can say let's actually do it over here so we can do a find let's get rid of that so DB post find and then we can do money sign text open some curly braces and then in here we can do search and we have to do some escaping here so we're gonna do a slash like that and then let's do post we'll just do au and then slash and then some quotes so what this should do is it should do a text search for this right here which obviously should come up with post one so let's paste that in and run it and you can see we get post one we can do that we get post one now if we run the same thing here but we do post T we should get post two and three it should match that so let's run that and you can see we get both post two and three so that's how we can do a text search all right now I mean there's a ton of things we can do I'm just going to show you a couple more and then we'll move on but we can do greater than or less than so let's do a quick update we'll do d be dot post update and let's add a view z' property to post to so say title and post two and then for the second parameter here we want to use the set operator and we want to update views or our in this case we're adding views and it'll say ten all right so now what I want to do is greater than less than so we can do DB dot posts and we want to do dot find and inside here I keep hitting Enter inside here let's put views and let's say we want where it's greater than we'll do greater than let's see I forget what we have in there let's do greater than three oh I forgot wait what did I forget oh the money sign okay so that gives us both just I can't read this all right so we got ten views here and this one has six so let's do greater than six so we'll change the three to a six and we should just get post to which we do and then you can also do greater than or equal so that would just be GTE so if we do GTE six and you can also do LT and LTE which is less than and less than or equal to all right but that I mean all the stuff is in the documentation I just wanted to give you the basics of you know crud create read update delete and then some of the operators that you can use okay so now that we've done that and to get out of this we can just do exit that'll get us out of the shell but let's go ahead and take a look at Atlas now so I'm gonna jump into well actually before we do that let's take a look at compass because I just want to show you that it'll show it'll show us everything we just did just in a graphical interface and if we go over here to Acme you can see we have the number of indexes we have two indexes we have three documents gives us the total size if we go and post so we can see everything that we've created we can easily changed stuff so if we do you know edit we could change this to a 1 and update so compass is really useful to just quickly get in and see all your data and edit it and stuff like that but you really should get familiar with the actual you know the shell and the syntax because when you're using MongoDB within your web app or mobile app or whatever it is you're using your API you're gonna need to be familiar with the finds and updates and all that stuff alright so I'm gonna close that up and let's jump into Atlas now you're gonna have to create an account if you want to use Atlas so I'm gonna sign in with an account that I just created pretty much brand new account so I think it's support login here alright now once you log in it's gonna have you create a cluster so let's say build a cluster and you're gonna see a screen like this where you choose a provider so I always use AWS but you can also use the Google cloud platform or Azure and just choose your your region I'll use North Virginia then we have our cluster tier so basically this is like different packages now this is the m0 sandbox which is the free one so this is great for development environments but as it says it's really not suitable for production environments maybe for like a portfolio project or something like that but if it's anything that's going to have a you know a good amount of data then you're probably going to want to upgrade to a paid plan so let's go down here you can rename your cluster I'll just call this test test cluster 1 and create all right so this takes about it says it takes about seven to ten minutes but it actually takes like two to three minutes really so this is just gonna set everything up so I'm gonna pause the video and I'll be back when it's done all right so there's a few steps once we create our cluster that we need to take and there should be a little checklist down here yours if you just created the account this will all be white except for the build your cross cluster so the next thing is to create a database user to do that we go to database access and then add new user and it'll just say Brad password one two three four and then you have some some options here can be an Atlas admin read and write to any database so this user will be able to will have full control you know read/write access to all the databases or only read any database you can create custom roles temporary user things like that but we're just going to stick to this read and write to any database next thing you want to do is whitelist your IP address so you go to network access add IP address and you can add your current IP address it should detect it automatically or you can allow access from anywhere which obviously is less secure I'm going to just go ahead and do that just because I'm gonna delete this after anyway we'll go ahead and confirm and I think he should be it you can also load sample data which I'm not gonna do and then if we go back up to you know cluster this main page right here if we want to connect we click on this button and if your if you want to connect through your application you'll get this string right here which includes your username and then you of course need to put your password in here and if you took my react course on you to me or my myrn stack course or you know few might quite quite a few of my YouTube videos I show you how to do this and we connect and and you know you just basically need to copy this string and put it into your application whether you're using the MongoDB driver or Mongoose which which which is an OD M or something like that now if you want to connect to this database through the shell we can do that as well so we just click this right here connect with the shell and just copy this string which is the command and then basically the database you want to connect to so let's let's try that let's go over here and paste that in and let's run that and it's going to ask for our password which is the user I just created and now we're connected so we're now in the cloud database so if I say show DB's we just have this admin and local let's go ahead and create a new database so I'll say use we'll just call this Acme as well and then let's create a collection holder DB dot create collection let's call this one - duze so that will create a collection and this remember this is not local this is our cloud database our remote database and we can insert a document so we'll say DB dot - duze so you want our - duze collections collection insert and let's just give it let's just give it a text value of to do one okay and that should insert it now we could do a fine DB dot to do is dot fine so obviously we can do everything we do with the local but let's head over to Atlas and we should be able to see our data so if we hit right here collections it's going to retrieve our data and you can see we have an ACME database with added OU's collection and we have one document when we can edit this from here if we want to change it to like to do one update and if we go back to our terminal and we do a fine you can see that that's been updated so Atlas is is similar to compass except it's in the cloud it's not on your local machine you can also you know insert documents from here so if I say insert document and we say text let's go to let's say to do 2 and insert so that'll insert that and if we go back to our shell and we do a fine we should see that to do two okay and we can just exit out of this if we want so hopefully this gives you guys an idea of how to start using obviously this is just the database parts to use it with in your application you need to get familiar with the drivers and packages and all that good stuff alright so that's it thanks for watching if you like this please leave it alike and I'll see you next time
Info
Channel: Traversy Media
Views: 587,129
Rating: 4.9602714 out of 5
Keywords: mongodb, mongo, nosql, nosql database, node.js mongodb, database
Id: -56x56UppqQ
Channel Id: undefined
Length: 36min 42sec (2202 seconds)
Published: Tue Jul 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.