In the last section we had a very long discussion about how we would be using lot to uniquely identify
users inside of our application.
In this video we're now going to continue by talking about the basics of Mongo D-B and then in the next
section we will start to wired up inside of our application.
Once we have Lango D-B set up inside of our application we can then make sure that we create a new user
record every time someone signs into our application using cloth.
After that we can then get back into our waterflow making sure that we deal with this cookie thing and
all that kind of associated logic that we will make sure to review over time.
So let's get to it.
Let's start talking about the basics of Monga.
We're going to start by reviewing a diagram that we are looked at much earlier inside this course.
But with one small change to it.
So we had previously looked at this diagram where we had said that we have our re-act up running inside
the browser and no we haven't done this yet but we will very shortly.
We had said at the re-act up would communicate with our express API by exchanging age TTP requests that
contain little snippets of Jason data we haven't previously said that express would interact with our
Mongo divi database to store records and pull out existing records.
Now I've made one change to this diagram where I've added in the additional box here for a library called
mongoose J.S..
Mongoose is a optional library that we are going to be using inside this course.
The sole purpose of mongoose is to make our lives easier when working with Mongo D-B.
So again it is completely optional.
In general it is required in this course but in general it is optional but I highly recommend you use
it because it wraps up a lot of very common operations that you might have to do by hand when working
with Mongo.
So as we start to talk about Mongo inside of this course we're really going to be covering two separate
and distinct topics.
On the one hand we will be talking about Mongo D.B and on the other hand we will be talking about mongoose
and how we use it to interact with the data that sits inside of Lango.
OK.
So let's start that process now.
The first thing we're going to discuss is how Mongo internally stores information.
So here's Monga.
Mungo internally stores records into different collections every different collection that sits inside
of our database can have many different records and we can have many different collections.
So inside of one Mongo divi instance we might have a collection of users a collection of posts and a
collection of payments inside of a single collection like let's say the users collection.
We have many different individual records in the context of a user's collection.
We might imagine that every single one of these records represents someone who has signed in or signed
up to our application.
So we've got one record that represents a user with the name of Anna and another record that represents.
Alex bill and so on every single record is essentially a little piece of Jaison or essentially a plain
javascript object.
In other words every single record is a collection of key value pairs.
So I can have a name property with a value of Anna.
And let's zoom in here really quick a height with a value of 150.
And then in the next object over or the next record over I might have an ID a name and an H.
One of the important defining characteristics of Mongo D-B is the fact that it is what we refer to as
being schema Elance.
In other words inside of one single collection every record can have its own very distinct set of properties.
So you'll notice that for example this first record right here has a height property but the second
one doesn't nor the third but the fourth one does likewise.
The second record right here has an age property but no other record in here does.
This is in direct contrast to traditional databases like siecle type databases or relational databases
where traditionally every single record has to have the exact same properties.
So in other words everything would have to have an ID a name a height an age defined so that is one
distinct difference that separates Mungo DVH from other databases like say My sequel or postscripts.
And let me be clear One difference among many many other differences as well.
OK.
So that is a little bit into the background of Mongo D-B.
Remember we have many different collections in every collection has many different records.
You can think of every record as being a single javascript object that has a couple of different key
value pairs.
Now the next thing I want to talk to you about very briefly is what Mongoose is doing for us.
So we're going to first talk a little bit about exactly how mongoose relates to Mongo D-B So on the
bottom half of this diagram will focus on this bottom half.
First we had just one second ago said that in Lango D-B we have a bunch of different collections and
every collection has a bunch of different records to represent this type of structure in the javascript
world or in the expressed code or the no J.S. code that you and I are going to write.
We are going to have two different very important concepts that are implemented by Munger's.
So just to be clear these are like mongoose things up here.
Let me label this as being like this is from mongoose.
So in the mongoose world or by using the mongoose library we make use of something called a model class
a model a class created with Mongoose represents an entire Mongo DB collection.
So the model class is used to access a single collection sitting inside of Mongo D-B the model class
has a bunch of functions attached to it that are designed to work with an entire collection.
And so doing things like say creating a new record or searching all the records inside of our collection
is done by using a model class.
Mongoose also gives us access to something called Model instances.
And so to be clear again this is all from mongoose model.
Instances are all javascript objects that represent a single record sitting inside of a collection.
So in practice we end up with one modeled class representing one collection inside of Mongo DVH and
then we end up with many instances each of which representing one of the records inside of our Mago
DVD collection.
So again one more time because this is a very important distinction.
One class is related to one collection we have a bunch of instances each of which represent one individual
record.
OK.
So I think there might be one more slide here.
No that's about it.
So I think that's enough for a very quick introduction to Mongo and mongoose.
Now I do want to tell you right now that inside like the first half of this course we're not really
going to using mongoose in Lango a tremendous amount.
So right now we're going to go through a little bit of the basic set up and we're going to use it in
a very limited basic way but later on in the course like in the last quarter we're going to come back
to Mongo and we're going to talk about many more topics around it so as to make sure we're on the same
page about what we're talking about when.
OK.
So I think this is enough the introduction.
Let's take a break.
We're going to come back in the next section and we're going to start to set up our Mongo DB database.