MongoDB Complete Introduction & Summary

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this video in this video we'll have a look at one of the most important and trending databases you can work with as a web developer these days mongodb we'll have a look at what it exactly is how it works how we can use it and what belongs to its rich ecosystem because that's so much more than that little database you can install locally on your system so we'll have a look at all these things and after this video you'll already be ready to start using it in your next applications so let's start so what is mongodb well first let's start with the name it comes from the english word humongous which basically means that it can store lots and lots of data and that is why that name was chosen so humongous basically means that there is a plenty of it now in germany that's actually a bit of a strange name but just as a side note maybe you can find out why but this is where the name comes from the core idea behind mongodb is that we can store a lot of data in there and work with it in a very performant way so that we can write with high throughput and retrieve data very efficiently and fast i'm talking about milliseconds here which is of course super important for modern web and mobile applications where you typically use mongodb because you can deliver data to your users fast now i also got a video by the way where i compare nosql databases as mongodb is as you will learn and sql alternatives like my sequel so definitely check out that video and article to learn more about when to choose which in this video i'll obviously focus just on mongodb so how does it work then well it's a database and therefore it's all about well storing data we might have a shop database so in our mongodb database server which we start in the end you will learn how to install it and how to bring up such a server in this video on that server you can have multiple databases and let's say we have a shop database because we're building an online shop now in that database we then have something called collections we might have a users and orders collection if you're coming from a sql world collections would be tables so collections are basically the parts of our database where we put our data into now the data itself would be a row or entry in a sql database in mongodb world we're talking about documents which look something like this if you're working with javascript then you might recognize these curly braces that looks a lot like javascript objects and indeed mongodb uses a notation or a data format which they call bson very similar to json i'll come back to that in a second which looks a lot like json and actually also works a lot like it so we store such documents in there and one important thing and one key difference to sql databases is that these documents are schema-less which means as you can see here we can have one collection one table if you want to call it like that where we store different data in here we have one document which describes a user who has a name and an age and the other user only has a name because maybe we didn't retrieve the h data in mongodb it's no problem to store such mixed data in one and the same collection which makes mongodb very very flexible and it's especially great for example for growing businesses where you don't know which kind of data you will need in the end or you will be able to gather in like six months but you want to start now and you can basically store the data you have and if you collect more data in six months you can add it to the same collection obviously you'll have to work on your querying logic then and make sure you don't rely on data which might not be there for all documents but in general this is a great advantage or it can be a great advantage at least now i was referring to that data format which is called bsun which basically stands for binary json now json is a data format you know from javascript or from web development in general it's a data format that looks pretty much like what you see here like a javascript object in the end you could say now bsun binary json is in the end using the same format but it's binary encoded to make working with it behind the scenes on the server more efficient to make it work with basically all languages you might use to reach out or to connect to your database so it gives you more flexibility but you don't have to worry about that you in the end store objects in your database that look like that and you will see how we create them in just a minute now the cool thing we can see here is we're really flexible regarding the data format and i said that you can mix data structures in the same collection you can also have so called embedded documents so here we have one document right with name agent and address but the address is a so-called embedded document so it's a document in a document and that allows us to keep data where we need it and that's another important difference to sql databases there we split data across tables we might have a users table and then an address table and we would match them with the id but here the idea is that we put data that belongs together into one document now there are different strategies for distributing data too but generally you want to keep data together because that makes retrieving it and working with it much easier because you already got everything you need without complicated joints which cost performance or which can cause performance at least so here we got all we need and we can just retrieve it and we don't need to join multiple collections or tables you can also have multiple nested documents like for the hobbies here which is an array of nested documents so this is all possible this is really flexible and working with it is a lot of fun because especially if you're coming from a javascript world but you can work with mongodb just fine from php pi finance onto but if you're coming from a javascript world this is all syntax or code you already know and therefore working with it is really really simple and fast not the worst combination now one important note of course has to be made about relations since we generally keep our data in one place we don't really have relations across multiple collections though we can have them but if we have them we basically have to merge them uh manually we basically have to write queries where we retrieve the first documents and then reach out to the naval collection and find the fitting connect the documents on our own but often you don't have that split up approach but you can do that too in mongodb now that's enough of the theory for now time to get our hands dirty so let's visit mongodb.com and see how we can get started using it on mongodb.com we can see that we can get started for free which is not the worst if we click that button here we're taken to a page where we actually get suggested to get to use mongodb atlas now i will actually use that in that video mongodb atlas is a cloud-managed database so a database running in the cloud to which we still can connect from our local machine or from any app we're writing now you don't have to use that cloud solution though you can also install it locally in the top right if you click on get mongodb then you also start with the atlas selected but you can switch to the community server which is the freely available server you can install locally on your machine here you just have to choose your operating system and then download and install it and make sure to follow these installation instructions in case you're facing any errors or in case you just want to well make sure that you get it right now there's nothing wrong with installing that locally and it might be the first intuition you have but actually the atlas server gives you in my opinion uh first of all an easier way of starting because it's really simple it's also free to get started with that that's all important to mention and it's also more realistic because once you finished working on your let's say node.js app or whatever it is where you connect to mongodb once you've finished with that you will need some kind of deployed database and deploying a database just like any database is a more complicated process because obviously you want to have a scalable performant secure database and mongodb atlas does all of that for you it's easy to set up sharding there which basically is the process of using multiple servers that are connected together to store and retrieve and work with your data you also get replica sets there which basically means that your data is replicated across multiple nodes for fault tolerancy so that when one node fails or is under heavy pressure you can get the data from another node you also can retrieve it faster therefore because you can work with multiple nodes with the same data simultaneously so all these things come for free with such a setup you have to manage them on your own when working with the community server the enterprise server by the way is basically the same but with a commercial license so if you want to have a self-managed server and you want to earn money with it that would be the option to go or there too you can use atlas so i will use atlas because it's easier to get started and does a lot of heavy lifting for us you just need to sign up there but you don't even need a credit card and it will be for free and once you did sign up which i already did you either end up in the set up your furnace cluster wizard already or on a screen like this where in the end you can build a new cluster now here you basically set up your cloud database and we can use the standard settings here because these are the settings which will give us a free mongodb clustering database now you can obviously also switch to more advanced settings if you need that for production you can split your database across multiple regions worldwide you have the fastest retrieval time and latency for the different regions your users might be in you can choose from different cloud providers because it's cloud hosted and not on mongodb's own servers but instead they're using these cloud providers there you can use the region or choose the region where your database will be hosted in and i will leave the default make sure to choose an option with free tier available to well get this for free then you have to choose the cluster tier and here we only got one free clustered here which is this very simple one which is fine for development and testing purposes though but of course you can also ramp this up to get better faster clusters with more storage space where you can store more data and retrieve it faster and you see the prices here but i'll go with the free alternative and then you have some additional settings down there like the mongodb version now on the free cluster we are stuck to this version here now that will eventually also change in the future but right now it's 3.6 on paid clusters you can actually then all choose the latest version for example but for our purposes here switch back to the free cluster i'm fine with that version which is the second most recent now on better or more expensive options here tiers up there we could also add or backup and shard our clusters which is the process of using multiple connected servers and that's not to be mistaken with the replica sets where we have fault tolerancy sharding basically means that we get even more power because we basically duplicate the number of servers that are working together we don't just have multiple data storage nodes but we really have the power times two or three or how many shards we add so we could do that um if we have that tier or higher and so on so you can see what you can add we need none of that and therefore we can just give that a name you can go with the default or choose your own and then click create cluster confirm that we're not a robot and then let this spin up and now this will take a little while and in the meantime as it also suggests here we can go to security because we want to secure our cluster here and that basically means we want to control who can access it obviously we want to lock that down only our application should be able to access that because well we're probably storing some sensible data in there now what can we do here you can create different users with different permissions here give them username and a password you can then choose what they can do can they only read read write or administrate everything i already got a couple of users there as you can see you can add an ip white list and there you can basically control which ips are able to connect to that cluster now i got a couple of ips there and that might be or probably is empty for you and then you can just add an ip address and enter your current ip address of the local machine you're connecting to this so that you can um well work with that and you can also simply click add current ip to automatically retrieve that or allow access from anywhere which you don't want to use in production so we can confirm that add that ip address and we can ignore the other settings here for now so this now gives us a setting where only our local machine only we can connect to this cluster and only with one of the users we created here and if you forgot your password you can always edit this and even audit generate a secure one automatically make sure to well remember your password you will need it for connecting to that cluster of course now with that let's go back to overview and let's wait for this to finish before we then finally connect to this cluster and start writing some queries so that you can not only see how to get it all started but how it actually works so now my cluster creation finished and now it's time to actually connect now if you click on connect here you actually gotta get a little introduction how that works and most importantly you have to choose a connection method now what does that mean typically you of course use mongodb from your application you're creating and that could be an application written with node.js with php asp.net python whatever it is not directly from angular or reactor or so on because you will need a server-side application connecting to mongodb for both technical and security issues because you don't want to expose your credentials in your client-side code which would be the case in angular or react therefore we either have such an application if you click on connect to application you actually can choose which language you used and these are not all languages which are supported you can click on view all mongodb driver connection examples to get the complete list and if you click on that on node for example you get forwarded to a page where you find instructions on how to install and use the node.js mongodb driver so here you would have to basically choose your version to get detailed instructions you then find out how to connect in node.js with the mongodb driver and obviously you need to install that driver too but i'll have a look at this in a separate video they're basically for node is a npm package an official one which you add to your project and you're good to go and you can basically start importing the mongodb package here and access the client so that is how you would connect in that way but i actually want to connect differently so i'll bring this up again i will use the shell and that is basically a javascript based program which you run in your terminal or command prompt on your machine where you can write all these query commands and connect to your server be that a local server or that cloud-based server here and therefore you can play around with it in a very very simple way without creating a whole node application just to play around with it we also got mongodb compass and we'll have a look at this in a while this is basically a tool which gives us a visual interface which is great but we want to use the console because there we write the queries pretty much as we'll also write them in our apps in our node or php or whatever it is apps we would connect to mongodb so therefore we'll have a look at compass later with the graphical ui for now let's simply use the setup where we write the code which is close to the code you would use in a real application here you have to choose your operating system and then download the shell first of all so this will now download it to my system and installing it is very simple you can view the detailed instructions here to then also find out how to connect for that you just have to choose the version and if you just downloaded it you'll use shell version 3.6 or later keep that open let's now install that shell and the installation will differ from operating system to operating system in the video description you also find a link with some information on how that works in the end on mac you just extract a file and then you get a folder which contains the shell and you can move that folder anywhere you want and simply navigate into that folder in your terminal and start it and you started with this command actually to connect to this cluster for that given user here and on windows it's very similar by the way so i navigated into that bin folder that was extracted on mac on windows you'll have an installer once you walk through that you'll also have a folder where the shell is installed and then you execute that command which is in there you can just copy and paste in the command you got in that modal on the mongodb cluster but there is something you have to add on mac you have to add dot slash at the beginning now this will try to connect to your cluster and ask you for your password that password is something you set up for the user if you ever forgot it you can of course assign a new one and then enter that password here and it should now connect to your cluster now once this finished you should be connected to the cluster and therefore to the database server not to a single database now you can start running mongodb commands and of course you find all available commands in the official docs one useful one is show dbs it shows you all the databases you have on your cluster and if it's brand new you'll have these two now on mongodb which is pretty convenient you don't have to create databases with complex commands instead they are created on the fly when you need them you can switch to a database with the use command for example we can use first test here now we switch to the first test database even though that didn't exist up there if we now type show dbs we also still see it because it's empty but since we switched to first test here we can actually now use it with the db command and we can chain commands to that and now we are on that collection level right we're in the database if you remember that slide from earlier we now work with collections which then hold documents now let's say we want to manage a couple of users we can simply add such a collection also on the fly there is no special create collection command with db dot users if you worked with javascript or actually with most programming languages you know that dot notation from objects and it's pretty similar here mongodb which is why it's really fun to use it as a developer a lot of the way you work with that database resembles the way you write code so now i'm accessing the user's collection which does not exist yet in the first test database which also doesn't exist yet and now i want to enter a user and that is the part where then both the database and the collection will be created once i really start storing data now to store data i can run insert and now we have insert many or insert one by the way you have auto completion with tab insert one as the name suggests inserts one element into that collection in that database and now that element is uh like a json object we use curly braces here to define that object now unlike in json you now don't need to wrap your keys and double quotation marks you can instead just write name colon and then the value so name is the the key of that entry you're adding to the database and now the value here is max now these single quotation marks are only needed because max should be a string you're not limited to strings of course you can store booleans numbers you can even store files in there though that is typically not what you want to do you rather want to store files on a file system and store the path to the files in the databases but that's just a side note and let's also add a number field therefore separated by a comma my age if i now hit enter this created that object and it automatically assigned an id which you can see here itself another type a so-called object id type but in the end this is the id which was stored now it would be nice to also see that entry first of all let's again type show dbs and now you see that first test database which was added because we now have a collection in there you can also see that if you type show collections now here you see that user's collection which we created on the fly and in that collection if you want to see all you can just access db.users which is the collection and then find if you execute it like this it finds all the documents in there and therefore right now we only see one and that is the one document we created with our name and our age here you now see these double quotation marks around the keys to and also that automatically generated id so this is how we can add and find documents now often we of course want to find um just a subset of all the documents now for that let's first of all add more let's first of all clear the console so that it's easier to read with the cls command we're still in the database we're still connected i just emptied the screen and now let's simply add more and for that i'll insert many users now insert many simply takes um as many entries as we want so we can add a user here let's say name man u l h or let's leave out the age here to show that versatility let's add another document here an arbitrary person which i don't know chris h could be 25. and let's hit enter and now i get an error here the reason for that is that this insert many command here actually doesn't take all the documents separated by a comma like this instead we have to pass an array here so squared brackets let's add them here around our documents we want to add and now this succeeded now if i type db users find we see all three entries we see that manual is missing an agent still was added without issues to that collection now let's say we want to add one more user the users insert 1 and that user here will have a name which is lisa and the age is 26. so now we have if we type db users find we have these four entries or these four documents in there now let's say we want to find all the users with an age greater than 25 so that chris is actually not included well for that we can access this again and write find again but now don't hit enter find as pretty much all these query commands you can execute takes optional arguments and here we can pass a so-called filter we again pass curly braces but now we're not defining which element to enter but our filter criteria we could for example type h 26 to find all entries where the age is 26. and therefore we only find lisa because she's the only person in or the only document in that collection with an age of 26 but i wanted to find all persons with an h greater than 25 and for that we can use the so-called query criteria i add another set of curly braces as a value for the age here and in there there are a couple of reserved operations or helpers we can use and you can find them all in the official box these all start with a dollar sign and then there is the gt the greater than helper and now i can define the value which is the value i want to have or i want to find records which have a greater than value so if i enter 25 here now i find lisa and max but not chris because 25 is not greater than 25 obviously and this shows you how easy and fun it is to work with mongodb and write more complex queries it's not super complex to be honest but it is a good start now let's add some nested record for that i'm clearing the window again i'll again reach out to my users and insert one user and that one user will have a name let's say susanna will have age let's say 30 and we'll have an address which then simply is another document created with curly braces here and that will have a street first first street and then let's say a city um let's say that is new orleans i don't know if that exists that address by the way let's hit enter if we now find all users again we see susanna there with the nested address now we can also run queries on such embedded documents for that we can again find and now let's find all users where the address street is first street we only have one such record we only have one record with an address anyways but still i just want to show you how that works for that we again add curly braces to define our search credit criteria our filter here and now in there we need to use double quotation marks and then address which is the field in the document in the user document itself which then holds the nested document as a value then we use dot notation in here which is why we need to double quotation marks and then the nested field so the street and then again just the value like first street here if we now hit enter we find that one entry that matches this and if i type something else like here have an error in the street we don't find anything of course so this is how we can insert and find our documents now obviously a common task also is to update or delete them now let's start with updating for that we have a number command the update command and for that command here we basically need two arguments the first one identifies which document to update the second one defines how to update it and then there's a third one where you can also configure the update mechanism for example define what should happen if no document is found should it create one or do nothing or if multiple documents are found for your filter should it update them all or just the first one but we'll focus on the first two arguments only the first one identifies which document to update and let's say we want to update the user max so we want to update the document where name is equal to max and then the second value here for the update command is how do we want to update it and now there's one important thing we can update in two different ways the first one is that we use curly braces and then dollar sign set and then we define in another set of curly braces which field to change in which way for example if i change h to 30 here and then i do db users find we see that for max the age was changed to 30 but please note that the name was kept if i repeat that command without dollar sign set so if my whole second argument is just age 30 then you will notice that it still updates but if i find all users the name is gone please note that the id was not changed the two ids here are equal but the name is gone so the update method actually differs if we use dollar sign set we do a patch update which means we only change the fields we defined to set if we don't use dollar sign set but we just pass a new object as a value this new object will replace the old one but we'll keep that old id so it's still the same document but all the values have been replaced and values which were included previously but are gone now will simply be dropped so this is the update command and finally if you want to delete something well then we can use db users delete and then there is delete many or delete one now delete one is straightforward you again have to search for what you want to delete and that works in the same way as it does in update and as it doesn't find by the way there also would have been find one and so on if you just want to find one element instead of all matching but that's just a side note let's go back to delete one so let's say we want to delete the user with the name chris if i hit enter here you see it deleted one so this seemed to work and if we now find indeed we only have four records instead of five and chris is gone now of course there are other commands and a lot of ways of using all these commands and i strongly recommend diving into the official docs and just play around with them as you see it's really easy to play around and you can't break anything it's just a sandbox database we're using here anyways so this is how we can play around with it in the console but of course sometimes it's also nice to have a graphical user interface and we can get that too with mongodb compass for that let's connect again and let's choose connect with mongodb compass choose your operating system and download it because this is a tool you install locally now as you can see it's quite big because it's a well full uh program with a graphical user interface and a lot of logic attached to it so let's wait for this to finish and then simply walk through the installer and we'll be able to then reach out to our cluster and inspect it with that interface in just a second so once you installed it and there should be a straightforward installation process for your given operating system you can connect to it now you'll probably use the latest version if you just installed it so you can copy that url to connect to your database so let's copy here you'll need your password here as it of course says and if you copied that string from the previous page it actually should ask automatically if it should use that though i also need to agree on the license agreements yes this is how it works thanks very much and it should now have populated that with some basic information if not you can basically edit manually now in here choose your username the password which you have set up on the mongodb cluster leave that authentication database here because that is the authentication database you have created the user on typically now if that fails also try creating a new user and make it an atlas admin and with that added you should be able to connect now once you're connected you see all the databases running on this cluster and therefore we also see the first test database and if you click on that you see all the collections also here on the left and if you click on a connection you of course also see all the documents so here are documents we added with the nest data and if you want to insert a document here we can do that too so we can also now edit all the data in here we can also create new collections in here as you can see we could add our products collection and that allows us to work with this in a nice and easy way though it is super important to know all these commands you saw in the terminal in the shell because these are the commands you will also write in your applications which you then connect to mongodb so that is mongodb in a nutshell how we work with it how we manipulate data and how it thinks and works and in my opinion it's really easy to work with it as a developer it's so close to how you write code in my opinion much easier to work with it than with sql databases though of course that's just my opinion now mongodb is actually more than just the database though or even for the database there's more than one option and that is what i want to conclude this video with if we are on mongodb.com we just got started or downloaded mongodb but if you click on solutions you actually see they got a couple of other products too now what's that all about mongodb enterprise advanced and for oem are basically also just the database the database servers for you to install on your own servers and to work with them so not the cloud managed version just a commercial self-managed database and ops manager and cloud manager are connected to that because these are tools which allow you to manage your database manage your own shards configure it secure it all that stuff you have to do if you're running your own databases and which then is made a bit easier but still database administration is something we developers probably won't do because it is easy to mess up and something you probably should get right that is why we have mongodb atlas which makes that a lot easier and which for example allows you if you get a great app idea to focus on your code and don't worry so much about the database now we also got other options here though we saw compass that is our graphical ui but what is stitch mongodb stitch is a pretty cool offering by mongodb which is only loosely connected to their database stitches you could say their serverless services offering now if that's not telling you anything serverless basically does not mean that there are no servers involved instead these are services which are all around you don't need to manage any servers not only database servers but also application servers you can focus on your code to be more concrete stitch gives you a couple of features which allows you for example to query the database from anywhere including angular or react apps and it does so by in the end giving you some hidden api which you normally have to write on your own with node with php whatever it is it gives you that and simply exposes you some functionalities you can execute safely in your client-side code where people are not able to get your credentials and start working with your database all of a sudden instead in the end you get an invisible api you could say stitch functions is a serverless offering which is completely decoupled from the database here this is basically something like aws lambda or firebase cloud functions so it's code in the cloud you write some code um which you then just upload there which you can execute which you can trigger and which does something something that can interact with the database but doesn't have to necessarily stitch triggers then works great together with stitch functions because stitch triggers allows you to run your functions in response to certain database events let's say you have a database where you manage user signups when a user signs up you maybe want to send an email to that user and you could use stitch triggers to listen to changes to your user database and whenever or to your user collection let's say and whenever a new document is added so whenever a user is signed up you send an email to the email address which probably is stored in that document and you do so by using a stitch trigger which runs a stitch function which then in turn does send that email through some third-party service stitch mobile sync is something which is coming soon and which is related to another solution mongodb mobile mongodb mobile is mongodb running on a mobile device and the idea behind that of course is that connectivity always is kind of an issue on mobile devices maybe your users don't have an internet connection maybe they only have a slow one well if you have the database on the device then your users can interact with your app to some extent even without an internet connection they can for example create new posts and you store them locally and then once the connection is back up you sync them to the cloud and mongodb stitch sync is a service that makes that syncing process fully automated and easier which will be available soon so that allows you to write apps that work great on mobile devices or anywhere and where you can sync your data around and therefore hopefully provide a great user experience mongodb charts is another offering where you can easily connect charts to your database so you have a web interface where you can create donut or pie charts or line charts bar charts on your database data without you writing any code and you also got some connectors which allow you to connect other business intelligence tools like tableau to your mongodb data so that is the rich suite of things mongodb offers the database is their core offering and almost all our services are connected to it in some way but in general mongodb also is a bit of a broader platform offering or with the goal of offering you everything you need to build modern web or mobile or desktop applications with a strong and very fast and fun to work with database as you saw and a couple of other things that hopefully make your life easier so that's mongodb in a nutshell i hope this was helpful and definitely let me know if you like this if you want to see more we can do videos on how we work with mongodb from for example node.js and so on and of course i also get a couple of resources already like my main course where we write an app an angular app with a node express back and then mongodb and so on so there are resources already if you want to see more let me know have a great day bye
Info
Channel: Academind
Views: 149,011
Rating: undefined out of 5
Keywords: mongodb, mongo, mongo db, mongodb tutorial, mongodb introduction, mongodb overview, mongodb summary, summary, tutorial, introduction
Id: VELru-FCWDM
Channel Id: undefined
Length: 41min 17sec (2477 seconds)
Published: Tue Aug 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.