Getting Started with MongoDB Atlas - A Modern Database!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone we're going to go through an introduction to mongodb my name is Jesse Hall I'm a senior developer Advocate at mongodb you might also know me from YouTube as code stacker so let's take a look at our agenda we'll first look at some document database Basics we're going to create a free mongodb Atlas cluster then we're going to load some data into our cluster into our database from a Json file I'm going to clone an e-commerce project and then we're going to connect that app to mongodb atlas so the first thing that we do need to understand is how data is structured in SQL or relational databases and you can think of these as spreadsheets the data is stored in rows and columns and so related data gets stored in Separate Tables to prevent data duplication because when relational databases were first created data was very expensive to store and so we didn't want to duplicate any data and in here this is an example of a relational database we have a users table and then a table of phone numbers that are related to a user using the user's ID and so user 1 has two phone numbers and user two has one phone number and so now when we want to query this data we have to access both of these tables and join the data back together now a document database is fundamentally different a document databases store data in documents so Json documents to be specific if you're familiar with JavaScript and you're going to be right at home because Json is Javascript object notation and so these are stored in key value pairs but it's very flexible and the values that can be stored are many different data types like strings numbers booleans arrays and even nested objects so we can store objects within objects or documents within documents we can even store related data in the same document eliminating the need for separate collections or tables so here's the basic Syntax for a document we have our opening and closing curly braces and then our key value pairs separated by colon now this is the same data structure from our SQL or relational database example that we looked at earlier and this is in Json format so we have our user's name our users phone numbers and we see that both of these phone numbers are listed here in an array and so the data again is structured differently than in a relational database so if there's one thing that you take away from this it's this data that is accessed together gets stored together and so we have to think ahead uh think of how our data is going to be accessed is if data is accessed at the same time it should be stored together most likely and that will result in Faster queries now let's take a look at some terminology here differences between relational databases and document databases like mongodb so in both relational and document databases a database is a database and in a relational database we have a table whereas in a document database like mongodb we have a collection in a relational database a row in mongodb would be a document and in a relational database a column in mongodb would be a field so when I refer to collections documents Fields you know what I'm referring to now how do we access data in mongodb so we have the mongodb query API and in this we can query and update data we can quickly query and update documents in our collection with powerful operations like dollar update mini and dollar set we can index to optimize our queries with many different index types for example compound multi-key wild card geospatial and more No Matter How Deeply nested the fields and we can transform and analyze our data we can create highly sophisticated real-time analytics against live data in the same database cluster with no ETL or duplication we can use search we can deploy Advanced full text search capabilities built into mongodb mongodb has built-in leucine powered a full text search with no additional infrastructure to manage we also have a graph so we can use operators like graph lookup to quickly Traverse connected data sets we also have join and Union we can utilize the power of aggregation stages like dollar lookup and dollar Union width to join and blend multiple collections for analytics and data exploration so let's compare queries between SQL and the mongodb query API so in SQL we would have select star from people so this is going to select everyone from the people table in mongodb if you're familiar with JavaScript this is going to look very easy to read so we have DB which is the database that we're connected to dot people is the people collection and then we're going to find and then we have empty curly braces which indicates that we want to find everything so if we take that a step further and we select start from people in SQL and then where age equals 25 in mongodb we would just put that into the find in an object so we'd say age colon 25 so that's the same as saying the age equals 25 and then we can also do a greater than so in SQL select star from people where age is greater than 25 in mongodb we would add to that the dollar greater than operator so that's dollar GT and we would set that to 25 so that's going to find anyone with the age greater than 25. and so you can see here in the mongodb query API is structured as objects and it looks very much like JavaScript so it's very easy to read now we have many different client drivers we have drivers for all of the C's go Java node.js Pearl PHP python Ruby Scala Swift rust and R so it's more than likely that you're going to find a client driver for whatever language that your application is using so that's it for the slides next we're going to jump into the live demo and we're going to see mongodb in action we're going to create a mongodb database load some sample data clone a repo and then connect our database to our application all right first off we're going to go to manga to be Atlas if you don't already have an atlas account you can sign up also it's free forever to sign up so in Atlas we're going to build a new database so let's go ahead and click on build a database and now we have several options here we have serverless which is our pay as you go option uh this one will seamlessly scale up and down to meet the demands of your application so if for instance you have a big demand it will automatically scale up for you if for some reason you don't use your your database at all for an entire month and it's not going to cost you anything are dedicated or is for production applications you get multi-region and multi multi-cloud options and this is the standard for most production applications but for today we're going to use the shared database this one is a no credit card required ever it's completely free it's great for Learning and for very basic use cases and so we're going to go ahead and create this cluster here and first thing we can choose our cloud provider we can choose between AWS Google cloud or Azure and we can also select the region that's closest to us to make it a little bit faster and so I'm just going to leave all of these defaults here and create my cluster all right so our m0 cluster is provisioning it's going to take three to five minutes while it's doing that let's go through our quick start here now by default mongodb is completely locked down so even I can't access it yet there are no users and there's no network access by default so we need to create our first user so I'm going to enter username of and the password of super secret we can also Auto generate a secure password just make sure you copy it and put it somewhere safe so let's create that user and then we also need to add an IP address to our access list again even though I have a user I still can't access it because the network is locked down as well so I'm going to add my current IP address and then finish and close and let's go to our database so we see our cluster is still being created while it's being created let's go ahead and move over to our repo that we're going to use all right so in this repo this is the jumpstart series that we have on YouTube where we build out an e-commerce website and we're going to use a branch from this repo for this introduction and so we'll put a link to the repo as well in the chat so that you can check this out but there's several branches in this repo and we're going to go to The Compass branch and in The Compass branch there is a products.json file we're going to use this file to load some sample data into our database so I have this file here and we can see this is a Json file we have objects here with a bunch of products so I'm going to go to Raw and then I'm going to right click and save as and I'm going to save this on my computer now to load this data into our database we're going to use compass compass is the GUI for mongodb you can download and install it for your operating system it will work on Windows Mac and Linux and if I forgot to say this is completely free now to connect the compass we need to get our connection string so let's go back to our database and it looks like it's done provisioning so now in our database we're going to go to connect and then Connect using mongodb Compass and we need to get this connection string here so I'm going to copy that now let's open up compass and we need to paste in our connection string in order to create a new connection so I'm going to paste that in here and the only thing that we need to change is our password so it automatically put in my username but I need to put in the password now we'll hit connect and now we're connected and let's go over to databases and we can see there are a couple of default databases you should never have to mess with these we're going to create a new database so up here in the top left create database we're going to name the database store and the collection name is going to be products so again this is an e-commerce site so this would be appropriate names here store and products so let's create this database and collection okay so now we have our store database let's go to our products collection and we can see that there's no data here so let's import our data so we need to select that file that we just saved so I'm going to select that file and open and this is a Json file we have a choice between Json or CSV let's go ahead and import and it's going to be really fast because there's only 16 products and so now we have our products in our database now Compass is great for many other things we can create aggregation pipelines here schemas explain plans indexes validation there's so much more to Compass there's even a built-in sh here the shell and so we can actually type out commands here as well so there's a lot more to Compass for you to explore but that's all we're going to use it for today all right so now let's go back to our repo we're going to clone this repo locally so we'll go over here to code and copy this string here and then let's go over to vs code all right in vs code I can go over here to my source control and because I'm logged in with GitHub I can clone a repository straight from here so I'm going to paste that repo URL here and hit enter and then I'll select where I want to save it it's going to clone it locally and then I can open up the repo okay so now remember that this repo has many different branches so what I'm going to do is change to the quick demo branch and you'll see this looks a lot different now now that we're on the right Branch this is a next.js application now I don't expect you to know react or next JS we're going to look at how mongodb is connecting and how it works and it works the same in all the different Frameworks so first thing let's take a look at our package Json and we'll see our dependencies we have react react Dom next and mongodb we'll take a look at how mongodb next but first off let's go ahead and open up our terminal Let's do an npm install and get all of our dependencies installed all right so while that is installing let's take a look at our environment variable file so now in order for mongodb to connect we need to give it a connection string just like we used in compass so in our env.local.examplefile we see we have a mongodb URI here that we need to enter now first of all let's go ahead and change the name of this this is going to be .env.local and then we need to give it the URI so again back in Atlas in our database we're going to go to connect this time we're going to connect your application we're going to select this connection string now back in vs code let's paste that in here and again we need to change our password and so let's save this file now that we have our connection string in there we should be able to run our application and see if it connects let's open up our terminal and we'll do npm run Dev and we should be able to open this up on localhost 3000 and see if it's working all right this should just take a few more seconds to run to compile and load and now there we go we have this data coming in all these products these are coming from our database let me zoom in a little bit here so nice we have our store and all of our different uh techware pretty cool so let's take a look now and see how this is being pulled into our application so let's go back into vs code and let's look at how this is laid out so so remember we had in our package Json we have our mongodb package let's go up to lib and then mongodb.js and this is how we're connecting we're using that mongodb package to pull out our client and then we're grabbing our mongodb URI environment variable that we set up just a little bit ago and so now if we don't have that URI then it's going to throw an error and say you know please add your URI to your env.local and if we do have it then it's going to create a new client passing in our URI and some options and then we're going to have our client promise which is going to connect and we're going to return then that client promise which can be used in the rest of our application so in about 32 lines of code minus all of the comments that are in here as well we're connecting to our database and now the rest of our application can use this client promise so now we're going to use that in our next.js application index.js it's structured under pages and then index.js is our main page so let's go here and we can see in here that we have our client promise that we're pulling from that lib mongodb file now in xjs we have our home function here which has and is connected and a product so it's pulling that in from the server side so next.js we can do full stack applications and so because next JS has this git server side props function this is going to run on the server and we are going to use that client promise here within this server side function so we're going to get our client first then we're going to check to see if our client is connected and then we're going to get our database so we're going to say DB equals client.db store remember we called our database store then we're going to get our collection so that's DB dot collection and then products that was the name of our collection and then we're going to get all of our products so we're going to say products is going to await collection.bind empty curly braces so that's going to find everything now this returns to us a cursor so we want to change that into an array so we'll say to array now we have our array of products then we're going to return these to our next JS client side so in our props we're going to pass in is connected so we're going to tell the client side whether we're connected or not then we're going to also return our products normally this would just be products by itself but because next.js is a little strict we need to sterilize our data so we're going to Json stringify our products and then Json parse them again and that just makes next.js happy that we have sterilized data again this is just the next GS specific thing so now again we have our is connected our products being returned to our home component right here where they're being destructured now we can use these within our application so we're using is connected right here so we're saying if we're connected return the rest of this and so that is the rest of our application and then products we're using products down here where we're sending that to another component a products component where the products are then being rendered and I'm not not going to drill down into all of the different components but that's the basics of how we're connecting and we're passing in our data and then displaying all of the data within our application so let's see what else we can do here so within our git server side props again right now we are returning all of our products let's say that we only want to return a specific category well let's go back into Atlas and within Atlas we can browse our collection here and so we have our store database and then our products and we see all of our data here within mongodb Atlas online and so let's see we've got category of Hoodie category of sweater mask t-shirt so let's pull in all of the hoodies so category is Hoodie let's go back into vs code and so right here within the curly braces we could Define our object that is going to specify the category of Hoodie so we just type that out category is going to be hoodies so hoodie just like that we'll save that let's go back to our application now we don't need to reload it or restart it but we do need to refresh it so let's refresh and now we should just have our hoodies we have two products here that are defined as in the category of putty so that is the basic intro of how to create a mongodb database load some sample data and connect it to your application
Info
Channel: MongoDB
Views: 27,810
Rating: undefined out of 5
Keywords: mongodb, javascript, da
Id: bBA9rUdqmgY
Channel Id: undefined
Length: 19min 56sec (1196 seconds)
Published: Tue Sep 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.