MongoDB Crash Course 2021 for beginners [ DataGrip Software ]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on everyone this is back brace welcome to the mongodb crash course this course will give you great understanding of mongodb concepts needed to create and deploy nosql database and this is for everyone who is willing to learn mongodb in simple and easy way we'll take a look to a few slides first where i will explain the main concepts of mongodb then we will jump to the installation of mongodb compass and the shell and finally we will practice some of the most important commands and queries that you can use in mongodb now i will be using an ide called data grip and i'll tell you why later but you can run the same thing on your shell and i will show you everything in a bit the prerequisites in order to follow along is that you should have at least the basic understanding of what is sql and what is nosql systems the difference between traditional relational databases versus document-oriented databases such as mongodb at least the very basics and if not then don't worry i'm going to try in this video to fill any knowledge gaps that you might have in this regard so by the end of this course you will feel more confident than before when it comes to nosql in general and mongodb in particular so what is mongodb mongodb is a non-structured database management system and if you're aware of sql which stands for structured query language this is a relational database management system where everything consists of databases inside them there are some tables each table has a primary key and foreign key they're all connected and related together you can run your queries between tables and you can get the result this is not the case in mongodb it's an unstructured database management system at least this is what you should know for the moment it's a cross-platform which means it works on different operating systems such as mac windows or linux document oriented database means that it's working on a document system and document is the equivalent of a row inside a table in sql or in relational database systems also it provides high performance availability and easy scalability and we have covered these points in the other video where i talked about the main differences between sql and nosql so make sure to check that video out there is no one master computer in the middle where everything is concentrated but instead there are multiple replicas with copies of the same database everywhere and therefore you can get a high performance without blocking also availability which means that it can run on the cloud so it can be deployed everywhere in the world and you can have your team scattered all over the globe an easy scalability means that it can be horizontally scaled mongodb works on the concept of collection and document instead of table and raw a collection in mongodb is similar to a table in sql and a document in mongodb is similar to a row or record or double you might hear me saying tuple as well all these words have the same meaning and these are only in sql now what about the database structure well database in mongodb is some sort of a container for collections so you can see here that we have one main database and we have multiple collections and each collection can have one or multiple documents so each collection can have one document or an amount of documents depends on you and how you want to design your database so to recap quickly guys collection is a group of mongodb documents and it's the equivalent of a table in a relational database system a collection exists within a single database collections do not enforce a schema it's basically schemeless so you don't have to design it ahead like in sql system where you have a table you need to have your rows and columns designed ahead you need to know what kind of attributes you want to put what the primary key is going to be what the foreign key is going to be this doesn't exist in mongodb so it doesn't have a schema or schema list but even this is not entirely accurate because even documents within the collection can have different fields for example you have an id here and this is auto-generated by mongodb this is an object id with 12 bytes character you have title you have description by you can have actually embedded documents inside other documents or just independent documents and we can refer to them in our main document okay so we can refer so let's say for instance that we have a document in collection 3 and we want to refer to that document we will refer to it by the object id so it's not entirely schema-less also you might have noticed that a document is a set of key value pairs and documents have dynamic schemas and what i mean by dynamic schemas is that the document in the same collection don't need to have the same structure so if we'll compare between relational database management systems in mongodb we can see clearly that there are some differences so the databases are the same primary key at the bottom are the same in rdbms or sql systems we have a table consists of rows and columns in the nosql system we have collections we don't have tables this is the equivalent of table also in sql we have rows or records or tuples in nosql we have only documents in sql systems we have columns or fields or attributes all these have the same meaning in non-rdbms or nosql systems we have fields so the field is the key in that key value pair system but let's take a closer look to a document let me give you an example of how a document which is analogous to a record in sql database may look like this is just an example for a document of a blog post so we have here this underscore id which is a 12 bytes number that assures the uniqueness of every document so this is a unique identifier for each document actually it plays the same role as a primary key in sql systems we have a title we have a description by can have url you can have tags and tags can be set to an array we can have an integer so we have varchar or strings we can have integers we can have embedded documents as i showed you so we said that we can have separate documents and we can refer to them or we can have embedded documents inside our document all right so everything here is in json-like format i refer to it as json format so what mongodb actually does is that it takes this document in json-like format and it compiles it to zeros and ones to bson or binary json in order to compile it and understand it so let's check out the advantages of working with mongodb as we said it's schema-less we can have different documents the number of fields the content and document size are really flexible we can have deep query abilities um dynamic queries on documents and we will see that in a bit when we'll start practicing ease of scale out horizontal scaling as opposed to vertical scaling in sql systems mapping to database objects not needed so we don't need any object relational mappers also it uses the internal memory so it's really good it's really efficient because the reading time from the memory is faster than reading from the hard drive why do we use mongodb well because document oriented storage it has a json-like format we have read replicas one computer with the original database system and we can have on the same table or horizontally different computers with cheap equipment really not very high standard that can have copy from that original database and they are highly available because it can be through the cloud as we said also we don't have any complex joins we can have embedded documents or reference documents so we don't do the joints like we do in sql systems also we can have rich queries and we'll see that when we'll practice and mongodb also has auto sharding which is analogous to partitioning in sql systems so where to use mongodb big data is one big field as we said before 90 of the data on the internet were only produced in the last two years also in data hubs content management and delivery in mobile and social infrastructure and user data management now in terms of relationships in mongodb relationships represent how various documents are related to each other and as i showed you before we can have one document and we can have independent documents so i mean we can have two independent documents in two different collections or two different tables um the way we can connect both documents together is either by referencing or by embedding so here we can see that we have embedded this document with building pin code city and state here in the same document so we used this code or this query db.users db is the database users is the collection and we access a method called insert so we want to insert everything together and when we will find or when we will retrieve the data that we have stored you will find that everything has been stored in one single document so the main advantage of embedding one document inside another document that it maintains all the related data in a single document which makes it easy to retrieve and maintain right instead of jumping back and forth between different tables you can have all the data in one single document so the last three slides i would like to show you some interesting facts about mongodb so you can see here that mongodb is on the fifth position after mysql postgresql microsoft sql server sqlite and mongodb is in the fifth position okay this is the survey for stack overflow for 2020. also mongodb is ranked the first for the most wanted database system in the market so if you're a software engineer or a back-end developer full-stack developer knowing and understanding and practicing mongodb is a must so the last slide in the presentation is the customers so more than 26 800 customers have chosen mongodb and the reason is obvious because of everything that we have said in the presentation so far and i work with sap on a daily basis and i didn't know that it's using mongodb as its primary database system also you have astrazeneca kpmg you have ea sports and this was very surprising to me i thought they're working with just traditional databases with rows and columns ebay facebook google thermo fisher barclays cisco you have also a bank of scotland uk government so you have very reputable great corporations really depending on mongodb in their database management system all right so this was the last slide now let's go ahead and start coding in the shell but before that let me show you in mongodb website how to install how to install the shell and compass and then we'll start working with shell alright so let's do it welcome back in order to download and install mongodb shell and compass you will need to have an account on mongodb.com so to do that just click on start free so the website is mongodb.com and you can fill here your company if you have a company also your work email first name last name and password once you do that you will be redirected to the home page of mongodb atlas all right so i have already an account i can sign in directly notice here in the middle create a cluster choose your cloud provider region and specs so your cluster basically will be responsible of maintaining your database on the cloud okay you can run your database on the cloud run your queries um having your everything basically mongodb you can have it on the cloud through that cluster so we'll build the cluster later but what i want to show you is how to download the software so we can log out now for the moment and we can go to mongodb.com and you can go to software community server not the enterprise but community you have also developer tools compass ops manager connectors click on community server alright so you will need to download that this is what mongodb offers so it offers here the community server version of the distributed document database once you download that we can go to tools so you will need to download shell and compass mongodb compass is a gui interface so this is a gui for mongodb that allows you to make smarter decisions about document structure right commercial subscriptions include all right what we're interested in really in this course is the mongodb shell so go ahead and download that it's a microsoft installer and this is only for windows 64. uh we have here red hat mac os ubuntu okay so choose your system and download the installer download the package okay and the installation is very straightforward once you downloaded everything successfully go and open your terminal or your command prompt in my case as i'm using windows operating system i have my command prompt and go ahead and say dash dash version if you will get this then mongodb is successfully installed on your machine okay so you have the version the get version modules allocator environment so you have windows system with x86 architecture all right now i'm going to use a software called data grip which is one of the many software products created by jetbrains let me show you the website real quick all right and you might know intellij for example so this is data grip um enjoy working with databases so different databases and only one system so you can run on that everything that you can imagine postgresql mysql oracle ibm db2 mongodb all right cassandra mariadb sqli3 all right so all of the sql systems that you can think of you will find them supported in data grip okay it's it's a very nice ide you might know intellij which is very popular ide for java let me show you uh solution developer tools there you go so you have here intellij this is for java phpstorm for php go land for go language this is our data grip pycharm for python ruby mine for ruby you can download it for a 30 days free trial or you can buy a license for it so in my case i have the complete version but believe me the same thing will work on your shell so let me just grab um data grip here let me just minimize this one bring it here and we'll have our shell here so one very important thing before we get started you need to connect to mongodb server in order to do that just type d alright now you are connected to check out if you're connected or not just run or sh they're the same and you should be connected and ready to run your queries so what we're going to do here will be the same exact thing here because i'm going to write queries with the curly braces and json formats not very convenient to do that in the shell that's why i decided to do it in data grip all right so this is mongodb compass and we should be able to see all of our databases with the collections and documents and everything now we can connect so here you can connect locally or on the cloud in order to login locally you can type db colon slash slash local host port 27017 this is the port for mongodb and let's see if it will listen on the port successfully and we are connected locally okay so for now we have uh these three databases this one i've just created just to show you what we can do and we'll create other databases so don't worry sample database we have one collection all right so the collection has no data or the collection has no documents you can import data if you want okay so from json format or csv format all right you can have your file can browse and select your file and import all of your data but we don't have that now let's just keep this open and i'll just put it there all right so let's check out what databases we have so far so to do that you can type show dbs all right you can do the same thing here in the console in in the in the shell show dbs and we have admin config and local these databases created by mongodb this one i just created is just taking a little bit of time and finally alright so basically we have the same result so this is to show databases you can also choose an existing database so let's say that we want to choose sample database what we can do we can type use sample database all right and you see here switch to db sample database i'll show you also here i'm not going to do this for everything but just uh in the beginning to show you that basically everything is the same uh what's working here is the same as here no difference sample data database switch to db sample database okay we can show collections in the existing database so to do that we can type show collections so we have one collection which is sample all right same thing also we can delete a database so in order to delete the database totally we can say so i will do it here in the shell you can type simply db.drop database all right so sample database is dropped or is deleted so to create a database we use the same command as choosing a database so it's the same command use so let's call it for instance newdb so we have created a new database called newdb so let's check that out and for the moment we don't have anything although it exists but because we don't have any collections that's why it's not shown all right so here also the same thing but just to show you which database we are in for the moment you can type db and there it is all right so let's go ahead and create a new collection so we can say db db.createcollection like that and we will have a collection of clients all right good with the green check mark here means that operation is successful and now if we will display all databases again we will have our new db shown in the list all right so this is for creating a new database to show the collections in a database all right this is to switch or to choose a database this self-explanatory i don't need to type anything just to show databases all right db this here to show which database we are currently in and here to create collections all right so our collection doesn't have any documents so let's try to insert a document in our clients collection we will take our database and we will access the clients collection and we want to say insert in order to insert whatever document inside that collection and we'll open parentheses and we'll open curly braces and here we can define any sort of fields that we want basically so we can have an id we give it zero we can have a company our company name we can call it la sosta for example we can have an address we can say 23 lost street new york city we can have also overdue invoices we can give that let's say 24 doesn't matter we can have an array so let's have an array of products and let's have coffee and sugar all right let's have date which is date type to display the current date we can have also contacts which can be a second document inside the main document all right so contacts contacts is going to be set to another json type so name is set to let's say jim neil um position owner and finally let's give him a phone number so let's say five five five double zero double one all right and that's it uh just the comma like organize this a little bit okay that's better all right and let's hit enter and it was successful now let's take a look to what we have so far so we can say so this is to query the data in order to find the data actually so we'll say db.clients.find and everything is displayed here so i'll make this a little bit bigger for you guys to see so we have an id this is auto generated as we said this is mongodb takes care of this we have an address a company name contacts so inside contacts we have the name position and the phone number all right we have a date we have an id that we have defined here above also we have overdue invoices 24 and products okay so everything is sorted very neatly and nicely here and the same thing exactly will go if you're coding with me if you're doing the same thing on the shell let's go ahead and do that together db.clients.find so i need to use new db all right now we can use the same command so there you go so we have an auto generated id the id that we have defined the company address overdue invoice everything all right everything that we have entered we have the same exact result okay so you should have this so now you know how to insert a document how to query a document now let's check out if we want to delete the document so if we want to delete a document we can do that very easily by saying db.clients.remove and we will remove using a unique identifier um try to avoid using something that might be redundant in different indifferent records or different documents because it's not a good idea to do that so the best practice is to take the id that we have created here the id 0. this is the criteria based on which you can delete a document or data so we can open curly braces we can say id 0. so i want to remove the document with which the id is equal to zero all right and let's run the query and boom so you don't have anything so if we will run this again you will not find anything similarly here if we will say db dot clients dot find you shouldn't get anything so we can also insert multiple documents okay let me show you that quickly just make like this for time's sake okay so we have different documents so we have db dot clients instead of insert we will use insert many okay makes sense we are going to insert the first document the second document and the third document okay the same exact field so we have id company address overdue invoices products and creation date okay and let's run that all right perfect so let's check that out okay so green mark means that everything went okay let's query that again dot db.clients.find and you can see below here that boom we have three documents with company name address creation date id everything is organized and neat and that's why i like really data grip you can use data grip with other database systems such as mysql or postgresql it's really great ide and let's check out here the same thing so we'll say database.clients.find okay oh take a look beautiful three different documents all right so let's check out for instance the company where the overdue invoices are 21. okay so how might we do that we don't know for sure because we might have different documents so for example we might have 100 documents in the collection this is just for simplicity's sake but if i want to check out the over the companies with overdue invoices of 21 for instance so how might we do that this is very easy so we can choose document based on a criteria in order to do that we can say db.clients.find the same method but inside we'll have an argument of for instance the overdue invoices which is equal to 21. so we will say over due invoices set to 21 and this should be inside curly braces like that right and there you go indeed the client with the id one has overdue invoices so what's the name of that client vanilla factory okay so this is how to pick a certain document from a collection of documents all right based on a certain criteria or something that you are looking for and this is very similar to in sql for example you would say something like select all from clients clients table where overdue invoices is equal to 21 for example over due invoices all right this is what would you do in sql system it's the same thing so let me just comment this out similar to in sql all right and i will leave this i will i will copy that guys and i will leave it in a sheet and i will keep it in github you can use that as a reference you can also sort data by ascending or descending order so what would you do simply is so let's just add a comment here so ascending or descending these set descending order it's just sorting right sorting by sending or descending order to do that is very simple you say db dot clients dot find and you want to access another method called sort so let's say for instance id zero oh yeah sorry for that um we use one for ascending or minus one for descending i don't know why i have chosen two or just i thought differently so we can use one for ascending and -1 for descending okay so you have descending because we use minus one and ascending because we used plus one or one zero one two two one zero okay i think i hope it will work yeah it will work so it's not always the fault of data grip okay we can also so here sending one descending minus one we can also update there are two ways for updating some element inside your collection or your document so the safe way which is you can set exactly a criteria based on which you can update and if you watched my farmstack course i have used the set operator to update the to-do item so let's say for instance that we want to update the overdue invoices instead of 22 we can say 40 for instance so let's see what we can do about that so here we can add a comment updating so in order to update we can use db.clients.update method and i want my criteria which is id number two i'll just make just like that all right id is set to two comma then open curly braces and here i will use dollar sign set which is the operator to set or to update whatever i want so i want to update the overdue invoices i want to update it to 40 and let's see if i will add something so it's a field that it doesn't exist in the document but what if i will say importer i will give it the name of let's say james co-limited for example let's see what will happen so ideally what should happen the 22 will be 40 and that new field will be added to the document so let's see what we can get query db dot clients dot find and let's take a look so let's make this bigger so here we have let's check out id number two okay and there you go we have updated the overdue invoices from 22 to 40 and there is the importer as well so you see what we did the importer in both documents are unset while the importer in the second id is set to jamesco limited as we defined here all right so i hope this is clear for you guys let's say that we want to increment uh vanilla factory overdue invoices from 21 to 50. so how much we do that so say incrementing we can take the db dot clients dot update and we want to update based on a criteria and the criteria here is id one so we'll choose the company with the id one and i want to increment so i will use dollar sign inc or short for increment and what i want to increment is the overdue invoices and i will increment it by right so let's run that okay successful let's check out again db.clients.find and let's take a look to the id number one okay and let's see we have 40 oh i think it's 9 29 not 19 all right let's say that we want to increase this from 69 to 70 so we want to increment it by one so let's increment that by one and there you go 70. we can also rename stuff so we can rename a field or a key in the key value pair so instead for example of company in id1 we can say legal name so in order to do that we can take the db dot clients dot update set the criteria id 1 and what i want to do is to have an operator rename and i want to rename the company and i want to set that to legal name okay so let's check that out db.clients.find and let's check out company we don't have something called company here but we have legal name which is set to vanilla factory the legal name is unset for both and is set for that vanilla factory um i meant to say that this is milk and say that this is sugar and coffee i don't know in the process we have changed this may be a bit confusing so let's actually work on that let's update that la sosta company from milk and vanilla to sugar and coffee so let's do that so in order to do that we said that we can use db.clients so here i'm going to update update array in id 0. so db.clients and i want to choose the id1 um id0 so you can do like that with the set operator and we can set the products so we can have here um the products and the products i want to set them to sugar and coffee okay all right so let's run that okay perfect so let's see if we will query to display everything so let's see what we have for id 0 and there you go they're updated to sugar and coffee all right so these are some basic commands that you need to master in mongodb certainly there are a lot more than that you can create indexes you can update embedded documents all right and in order to exit the shell just type exit and you are out now let me show you the same thing in compass so if you have compass opened you can take a look you can hit refresh here so we have our new db so we have our clients so basically you have the same thing id 0 id 1 id 2 with everything you have products array you can click on that arrow so yeah this is basically it guys um let me show you the last thing in this course how we can connect to the cloud so go ahead and sign in so the first thing you need to do before you build the cluster is you need to go to database access in order to create a new database user so we can have a user let's say back brace and your password all right and here the password contains special characters all right never mind all of that uh these are the privileges so you can have a total control read and write to any database atlas admin only read any database so this is kind of limited uh limited control to read only right but we want to have full control so read and write click add user now the next step is to go to clusters build a cluster so here you have different choices this is the one that we're going to choose so this is shared clusters this is actually for developments this is very good for development but not for production so if you're learning mongodb and trying to create some applications using mongodb database this might be your ideal choice you don't want to pay for extra bucks for something that you want to use really here you have dedicated clusters and this is the price and dedicated multi-cloud and multi-region clusters and it's with a higher price okay but we are going to create a cluster with this free tier that mongodb can give us you can choose your cloud provider and region i usually use google cloud others can choose amazon web services or microsoft azure um here i'm next to belgium i'm in poland so use um the server which is in belgium right so here you have a cluster tier with a shared dram and 512 megabytes of storage okay all of that's very good the cluster name you can rename your cluster so we can leave that as it is or we can change it we can say crash course okay and create cluster all right this is going to take some time says from one to three minutes all right so this took one minute or so you can check out your collections we don't have any collections yet okay and you can load a sample data set so here we have a sample data set for 350 megabytes uh if you want of course you can load that and check it out so let's see actually what they have available sample data set so we have airbnb geospatial restaurants and so on weather um so let's load sample data set actually and in the meantime let me show you if we want to connect so we have different methods of connection so you can click on connect so the first thing that you want to do is to add your current ip address i'm not going to do that you can add a different ip address or you can allow access from anywhere okay next you can choose a connection method so you have basically three ways to connect to your uh your cloud database the first one through your shell and you can take that string copy it and paste it in the shell okay of course with your username which is back brace here and this is the crash course database so you should be fine the second way is to use it inside your application so you connect to your application and you have the driver to choose from so you have node.js we have python perl php ruby okay of course with your username and you will substitute the whole thing okay with your password you can include that in your application or you can include full driver code example you can take the whole thing copy and paste it inside your code it will depend of course on the programming language you can choose 3.6 okay using pymongo all right so pymongo is a driver like motor and the third way is through your compass all right so let's see if we can connect to the shell let's copy that let's go to our terminal open that hit enter and enter your password that you have set for the user all right so now we are connected to the cloud let's try to show dbs so show dbs to check out all the databases that we have loaded from the data set let's use for instance sample air bnb so this is in the cloud guys right we are connected to mongodb server uh switch to db sample airbnb we can say show collections all right listing and reviews so let's check out everything in listings and reviews so we can say db dot listings and reviews dot find basically everything that we have used locally we can use on the cloud it's the same you hit enter and you have a lot of data there is a method called dot pretty to make everything pretty alright so still a lot but at least readable right we have here what do we have here so these are the reviews ben's apartment is situated among trendy cafes and bars all right that's very nice you can play with all of that alright so i hope this was useful to you guys and it gave you a glimpse on what mongodb can do and what you can do with mongodb actually the installation the queries everything about mongodb i hope that i have covered of course i didn't cover everything in queries and commands there are a lot of commands that you can discover the documentations of mongodb is amazing and again i recommend that you create one or two applications using mongodb and keep them in your portfolio so thank you very much for watching guys and i will see you in the next video take it easy
Info
Channel: Bek Brace
Views: 1,664
Rating: undefined out of 5
Keywords:
Id: EjcAqAJjmEo
Channel Id: undefined
Length: 48min 25sec (2905 seconds)
Published: Sun Jun 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.