MongoDB In 30 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video we're going to be covering MongoDB in depth and most of the content from this video is part of a 10 project course in MongoDB that I did so if you enjoy it I would suggest checking that course out too and I'll leave a link in the description so we're not going to be building a web application or anything like that using other technologies I really want to just focus on MongoDB and basically the syntax how to create collections insert documents update and delete documents create users and some of the other fundamentals of MongoDB alright so is what's called a no SQL database and there's different types of no SQL databases so to be more specific it's a called a document database alright data or records are stored as documents and they use JSON like syntax so if you don't know what jason is it's JavaScript object notation and I'll get into the syntax in a minute so notice ql databases are much different than relational databases like say mysql or Postgres with a relational database you really have to map out everything you need to figure out the exact schema including what tables you use what fields you'll use and even the types of fields ok whether it's going to be a string or an integer all that stuff so with no SQL there's there's not really any of that I mean you should plan out the structure of your database and your collections but you don't have to do any predefined structuring before you build your application alright one of the huge advantages to MongoDB and no SQL databases in general is scaling ok they're really easy to scale compared to SQL or relational databases they're also much faster in most types of operations there are some things that would it would benefit by using relational databases but when you're dealing with just a crapload of data then no SQL is usually the way to go all right as long as there's not really a ton of interconnected relationships all right so enough talking about methodology let's jump in and get started so I'm using Windows in the course I mentioned we do use Linux Ubuntu quite a bit but for this particular video we're using Windows so we're going to go to MongoDB comm and you want to click the download button here and that should take us to the download page and we're going to go ahead and choose this version here it says Windows Server 2008 that's fine 64-bit so we're going to go ahead and download that alright which I already did so it's 104 megabytes so it might take a couple minutes but I already have it downloaded so I'm going to cancel that and we're going to go ahead and run that MSI file so I'm going to click Next and just go through this looks up the terms now I think I'm going to choose custom because I want to change the location ok by default it's going to go in your Program Files folder and then DB server 3.2 I want it much more simple than that the path so I want to just go right to my C Drive so in my C Drive I'm going to create a folder called Luongo DB and then we're going to just open that and click OK ok so much simpler it's just right in the C Drive ok we'll click Next and then install and we'll say yes okay so that's finished we'll click finish and there's still a couple steps we need to do we need to open up a command line as administrator ok and I have that nice and big so you can see it and also we're going to go to that folder and C Drive MongoDB and we're going to create a couple folders here I'm going to create one called data and we're going to create one called log all right and then inside data we're going to have another one called DB ok and that's where all the data will be stored on the filesystem and now let's go to our command line here and we're going to navigate to the MongoDB folder and then into into the bin folder all right now in here we need to run D and we're going to add a bunch of flags here so the first one is going to be Directory / dB all right and then we want to specify DB path which is going to be the path to the the folder we just created so C Drive slash DB slash data slash and if you didn't install in your C Drive MongoDB just put whatever location you used all right and that's why I wanted to change that data that long location path alright so after that we're going to say - - log path and that's going to be C Drive slash MongoDB slash log and then I'm going to say slash dot log ok so that's going to be the file that all the MongoDB logs right - and then we just want to specify - - log append and - - rest and then - - install ok and that's going to allow us to run it as a service so let's go ahead and run that and now we should be able to run the service with net start MongoDB okay and now it says it's starting and it started successfully ok so we now have MongoDB installed and it's also running in the background as a service now we're going to be working in the shell and we can run that by simply typing from within the bin directory alright so we're now in the shell I'm just going to do think CLS and that clears everything out now to show us a list of databases we can do show DBS and it shows us that there's one default database they are called local which we're not going to touch we want to create a new database so to do that we're going to say use and then whatever we want to call it will say my customers and you can see that not only did it create it but it also switched us to that database and if you ever want to check what current database you're in you can just do DB and it'll tell you I just want to quickly go over the syntax for a document so I'm going to open up notepad and a lot of the queries that I do I'm going to put a notepad first and then just copy them and paste them in just for readability alright now basically when we're working with a document it's just like a JSON object or a JavaScript object we have set of curly braces and then we have a field with a value okay and then a comma and you can just keep adding fields so let's say if we have a customer we may want them to have a first name okay strings will have quotes around them and let's say last name all right so these are just very simple string fields alright of string values if you want we can also have an array as a value so let's say maybe there's memberships and we want an array with all the different memberships that this user is in that this customer is in so maybe mem one and M two and so on okay so we can have arrays we can also have objects so let's say address and that's represented with curly braces and you might have a street and a city and so on okay you can also have arrays of objects so maybe we'll have one called phones and actually is that that's not really a good example maybe contacts so if we have an array and you may have let's say named Brad whoops and then maybe a relationship friend and so on all right you could do a comma here and then add another one so you have different types of value now before we go and we start inserting data I want to just create a simple user for this database so if we go to I'm just going to search for it will say MongoDB create user ok so DB dot create user will create a user for that database and this format here so we're going to say over here DB dot create user and we'll pass in some curly braces and let's specify what is it user user and we'll just say Brad and then password PWD and roles ok roles I believe is an array okay can be an array of objects but I think we can make it more simple yeah I just want this right here read/write so that we can read and write to the database and also as an admin so I'm just going to grab that and it'll paste that in and then let's copy that paste that in and run it and now you'll see it says successfully added user ok so now that we have a user let's start to add some data so to do that we use insert so we say DB dot and then whatever like the Co we didn't create a collection did we all right so let's back up a little bit collections are very similar to tables in a relational database basically they're just they hold documents or records so to create a collection we're going to say DB create collection and pass in the name of the collection which will say customers all right and then if we want to see all the collections in this database we can simply say show collections all right now if we want to insert a document into that collection we say DB dot customers dot insert all right and then that's going to take in some curly braces and let's say first name John last name Doh okay so if we go ahead and run that we get an inserted one okay so it inserted one document now if we want to see the documents in a collection we can say DB dot customers dot find and you can see it gives us that list of documents which we only have one has a first name and last name but it also has this underscore ID field which is set to be an object ID all right and this is used as a unique value to find documents and do some other stuff but notice that it was automatically created okay so we don't have to worry about creating an ID field setting it to auto increment saying setting it as a primary key these are all things that you have to do in a relational database all right so let's go ahead and add a couple more now if we want to add multiple documents at once we can just make an array so put square brackets oops square brackets around it like that and let's change this name change them to Stephen Smith and then let's put a comma here and then another user or customer with the first name will say Joan last name will say Johnson okay now with a relational database we would have to specify that the customers table would have to have a first name and a last name and that's it they couldn't just on-the-fly add another field and that's what's great about no askew ellos we can do that so for this George own Johnson let's say we want to add a gender and we'll set that as females okay so set that is female and then let's go ahead and enter and now if we do a find you'll see that Joan has a gender field okay even though we didn't specify that for John and Steven it's fine okay you can have whatever fields you want on whatever documents you want all right now we only have a couple fields so this is this looks nice and clean but if you have a ton of fields here it can get kind of messy so we can add on to find we can say dot pretty just kind of a helper function and you'll see that it makes it nice and neat for us all right so let's look at updating a field all right so let's say that for John Doe we want to add a gender to him as well so what we would do is DB dot customers dot update and first parameter is going to be a match so we want to match name or actually let's do first name - John okay so it's going to match anyone that has the first name of John now if you're building a production application you probably don't want to use something like first name because other people can also have that name so it's going to update all of them you probably want to use either the object ID or some other unique field but just for simplicity we're going to use first name so then the next parameter is going to be what we want to replace it with okay so we'll say first name John lastname DOE and gender male right so we'll go ahead and run that it says it matched one modified one so if we look at the find now you can see that John Doe has a gender of male now notice when we did the update we had to also specify first and last name again even though it was already there if we didn't do that and we just said gender male it would it would basically replace the entire thing with just that gender male now there is a way around that and that's by using the set operator all right so let's say Steven Smith we want to add a gender to him all right so we can say update and let's say first name equal to Steven and then for the second parameter I'm just going to get rid of this okay we do want to keep the curly braces and inside there we're going to say dollar signs set all right and then we're going to set that to another set of curly braces and we'll say gender male so using set it's going to keep whatever is there previously and then just add this on so if we go ahead and run that and then we do a find you'll see that Steven Smith still has his first name and last name and just added the gender now we also have an operator called Inc which can increment numeric values for us so first thing I want to do is just add - Steven I'm going to add an age and let's say 45 actually we want to get rid of the quotes 45 all right so now if we do a find I can see that he has an age of 45 now if we want to increment that we can say DB dot customers dot update and we want to match first name Steven and then here we want to put our curly braces and then we're going to specify dollar sign Inc go like that and then we want to age and then the number we want to increment so let's increment 5 okay you just aged five years let's see what oh I didn't put curly braces around this all right so now if we do fine you see that Steven is now 50 okay it added five incremented now we also have an operator called unset if we want to remove a field so let's say DB dot customers dot update and we want to specify first name Steven and then let's put for our next parameter we're going to say dollar sign unset and we'll say unset I think we can do unset age let's try that nope age is not defined all right so we need to do unset and then curly braces and then age I think it's like that yeah okay so age one and then let's do a find and now Steven doesn't have age anymore now let's see what happens if we try to update something that isn't a match something that isn't there alright so let's say DB customers update and put in some curly braces and we'll say first name first name Mary and then we want to update we'll say first name Mary and last name Samson all right so let's go ahead and run that and you'll see that the result is zero all across so if we do a fine now it's just it's the same but if we want to say if this isn't found then insert it we can add an option called up cert so let's go back to that query and then we're going to set a third parameter here which is some options and we're going to say up cert set that to true and then we'll run that now you can see that we get one inserted or up sorted rather and it also includes an object ID so now if we do our find you can see that mary has now been added and an object IDs been created now we also have an operator called rename that we can use with update so if we say DV dot customers Tod update DV customers update and let's say where first-name is equal to Steven and then for the second parameter here we're going to say let's do curly braces and then rename and we should be able to rename let's say gender we'll rename that to sex I think that's the syntax rename oh not wait we got to put curly braces around this okay alright so now if we do a find now you can see that with Steve Smith gender has changed to sex alright so those are some of the operators we can use with update now to remove documents is very easy we can just say DB dot customers dot remove and then let's specify where first-name is equal to Steven okay you can see we get n removed one and now if we do a find Stephens gone now by default that query we just ran the remove it would delete all customers with the first name of Stephen but you could add a parameter here you could set inside your options there's one called just one and you could set that to true alright I believe it's true true or one but you can set that and then it's only going to delete the first Stephen it finds it's not going to delete all of them alright so just kind of a safety option alright so I want to show you a few other things but I want some more data to work with so I'm going to in notepad I'm going to just paste in a query to add some more data alright so we basically just have an insert here with some fields for a customer's first name last name gender age address which is an object memberships which is an array and then balance okay so we have what is it I think five four or five of them so I'm going to copy this and we're going to go over here and paste that in and run it you can see it's inserted five so if we go and we do find you can see the different customers that were added all right now I think up to this point we've just done find with no parameters so what I want to do is DB customers dot find and we can pass in a query here so let's say we want to find Sharon so we could say first name Sharon okay and that's going to give us all Sharon's information now let's say we wanted to find Sharon in Troy all right so what we could do is use the we could use the or operator so let's say DB dot customers dot find and let's pass in our curly braces and then we're going to say dollar sign or set that and then put some square brackets for an array and we'll say first name Sharon put a comma here and I'll stay first name Troy okay if we run that you can see that it gives us back Troy's information and Sharon's okay now we can do a simple find for a different field that is going to match multiple results so for instance gender if we say find gender and change that to male if we run that it's going to give us everybody that has the gender of male now we can also use greater than or less than operators so let's say we want to find everyone that is under the age of 40 so we'll say DB dot customers dot find and in here we're going to pass in age and in here we're going to set I'm going to open up some curly braces and we'll say dollar sign GT and we'll set that to actually do we want to do less than I'll do LT set that to forty okay and that's given us back actually let's add pretty to the end here okay so that gives us back everybody that is under forty you see Troy's thirty three twenty three and thirty five all right and obviously you can do GT for greater than all right now there's also LTE which is less than or equal to and GTE which is greater than or equal to now let's say we want to find everyone that lives in the city of Boston so you can see that we have for our address which is an object which has street city and state so what we can do is we'll say DB dot customers dot find and here we're going to say we're going to use quotes here and say address dot city and then here we'll say Boston okay we run that and it gives us two people that have their city as Boston okay when we're dealing with objects like this we have to wrap it in quotes you'll see if I go and take these quotes off it gives us an error and I should mention that there are some clients and programs that will require you to use quotes in the key as well so just remember that if you're having issues and some other client just try that and see if that helps alright now if we want to query the memberships okay remember memberships is an array then that's pretty easy all we have to do is customers dot find and let's say memberships and then just the name of it so let's say mem one okay so that's going to give us everyone that has mem one inside their memberships array alright now next thing I want to talk about is sorting okay so let's do a find we'll say actually I'm going to clear this out and we'll do DB dot customers dot find and then what we can do is we can add on to this we'll say dot sort okay and then that's going to take in whatever field we want to sort by so let's say we'll do last name so we could say flats name one okay so if I set it to one that means that it's going to be in ascending order so if we look at the last name we have Doe Jenkins Johnson Sampson Wilkins okay if we want a descending then we could just set it to negative one and then you can see whoops I want to do dot pretty okay now you can see it's going in the opposite direction okay we have Wilkins at the beginning all right so that's sorting we can also count documents so if we want to do DB customers dot find count and run that you can see that it gives us eight okay we can also put a query in here if we wanted to if we say gender male and it gives us four okay because there's four people that are males we can also limit so if we want to find we'll find everybody but we're going to set a limit I'll set limit to four and you can see that it gives us the first four and of course you can combine these so we could say limit for dot sort and we'll sort by city last name one and there we go another cool thing we can do is we can iterate through stuff using for each alright so we'll do DB dot customers dot find and then we want to say dot for each and then in here we have function we want to pass it a variable which can be anything I'm just going to say doc and then let's say we want to print it is a print function we can use and we'll print the string customer name and then we can concatenate onto that with a plus sign okay just like regular JavaScript and we'll say doc dot and then whatever field first-name and now we get we're printing out customer name and then whatever the first name is alright so we're going to go ahead and stop here now if you enjoyed this I would suggest checking out the ten project MongoDB course there are a couple projects like this where we're just dealing with the database in the shell but there's also projects where we're building web applications implementing different technologies like angular and nodejs so I would suggest that and thanks for watching and I'll see you next time
Info
Channel: Traversy Media
Views: 2,018,201
Rating: 4.9126387 out of 5
Keywords: mongodb, nosql, document database, mongo
Id: pWbMrx5rVBE
Channel Id: undefined
Length: 32min 11sec (1931 seconds)
Published: Sun Sep 04 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.