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 uh into our cluster into our database from a Json file we're going to clone an e-commerce project and then we're going to connect that app to mongodb app 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 uh 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 of a relational database we have a users's table and then a table of phone numbers that are related to a user using the users's ID and so user one 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 join the data back together now a document database is fundamentally different uh document databases store data in documents so Json documents to be specific if you're familiar with JavaScript then 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 uh the values that can be stored uh are many different data types like strings numbers booleans arrays and even nested objects so we could 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 user's 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 um and in a relational database we have a table whereas in a document database like mongod Tob we have a collection in a relational database a row in mongod Tob would be a document and in a relational database a column in mongodb would be a field so uh 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 manga Tob 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 uh many different index types for example compound multikey wildcard 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 uh full text search capabilities built into mongodb mongodb has built in loose seen powerered full Tech search with no additional infrastructure to manage we also have a graph so we can use operators like uh graph lookup uh 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 with to join and blend multiple collections for analytics and data exploration so let's compare queries between SQL and the mongod to be query API so in um 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 uh this is going to look uh very easy to read so we have DB which is the database that we're connected to 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 star from people in SQL and then where age equals 25 and mongod DB 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 Def find anyone with the age greater than 25 and so you can see here 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 Scola Swift rust and R so it's more than likely that you're going to find a client driver for whatever uh 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 uh clone a repo and then connect our database to our application all right first off we're going to go to mongod 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 um so if for instance you have a big demand it will automatically scale up for you uh 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 our dedicated is for production applications you get uh multi- region and multi multicloud options uh 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 is completely free it's great for Learning and for very basic use cases and so we're going to go ahead and create this uh cluster here and first thing we can choose our Cloud Prov 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 uh m0 cluster is provisioning it's going to take 3 to 5 minutes while it's doing that let's go through our uh quick start here now by default mongod GB 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 autogenerate a secure password uh just make sure you copy it and uh 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 uh 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 can 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 jump start series that we have on YouTube where we build out an e-commerce website and we're going to use uh a branch from this repo for this uh 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 uh we have objects here with a bunch of products so I'm going to go to Raw and then I'm going to rightclick 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 guey 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 it's completely free now to connect to 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 we'll go over to databases and we can see there are a couple of default databases you should never have to mess with these uh we're going to create a new database so up here at the top left create database and 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 uh so this would be appropriate names here store and products let's create this database and collection all right so now we have 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 pipeline here uh 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 and 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 VSS code 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 nextjs 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 uh we have react react Dom next and mongodb and we'll take a look at how mongodb uh connects 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. example file 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 uh this is going to be env. loal and 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 Local Host 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 Tech wear 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 mongod B.
J and this is how we're connecting we're using that mongodb package to pull out our client uh and then we're grabbing in 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. looc and uh 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 application so in about 32 lines of code uh 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 nextjs application uh in nextjs 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 an is connected and a products so it's pulling that in from the server side so nextjs we can do full stack applications and so uh because nextjs has this get server side props function uh this is going to run on the server and we are going to uh 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 connected uh 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. 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.