We've got our entire authentication system now put together.
So now we get to start to focus on recording different location points provided by a user.
Let's first understand the exact nature of the data that we're gonna store inside of our Mongo database.
OK.
Quick diagram.
So remember we're going to record a series of points as a user walks around the world.
So we're going to store the series of points inside of a record called a track a track essentially meaning
here's a track or kind of a path that a user walked around the world inside of every track that we store
inside of our database.
We're going to have the user I.D. That is the idea the person who created this track every track is
also going to have a name provided by a user.
And it's going to have a array of different point objects that we're going to store under a location's
property.
So this array of point objects is going to be the actual kind of mapped path that the user makes through
the world every point inside that array is going to have a timestamp.
So that's the actual time that the user was at each point in the world.
It's also going to have a coords object chords being short for coordinates inside this object.
We're going to have latitude longitude altitude accuracy heading and speed.
Now this object structure right here of having like a timestamp and then a separate object called coords
with all those properties inside of it might seem a bit strange.
The reason we're doing it this way is that this right here is essentially the exact object that we get
from a mobile device with React Native that describes a user's current location through essentially
just taking the information directly off the phone and throwing it into our database in the exact same
format without making any changes to it.
So again we're going to create a track every track is gonna have a user I.D. and a name and then an
array of those point objects so to start implementing this inside of our server we're going to first
flip on over to our code editor and we're gonna find that model's directory once again.
So we're going to create a new model so we can represent this collection of tracks inside of our database.
So inside of models I'll make a new file called Track dot J.S. than inside of here we're gonna write
out some code that looks very similar to what we put inside of user J.S..
So at the very top we're going to require in Mongoose
we're then going to use that to create a track schema
then inside of here we're going to list out all the different properties that we expect a track to have.
So as we just discussed first off it's going to have a user I.D. a user I.D. is going to be a type Mongoose
dot schema with a capital S types object I.D. and notice how object idea has capital ie lowercase d.
This is how we indicate that user I.D. is a reference to some other object stored inside of Mongo DV.
We're also going to give it a ref property of user the ref property is used specifically by Mongoose
and he essentially tells that this user I.D. is pointing at an instance of a user as was defined inside
of our user dot J.S. file that ref of user points it to this model string right here of capital you
user.
So that's how mongoose is going to understand that user I.D. is pointing at an instance of a user then
after that will indicate that this is also going to have a name property.
So that's gonna be a type string and we'll give it a default value of empty string like so.
So if for some crazy reason someone creates a track without a name assigned to it we'll have a default
name of just empty string.
And then finally we can indicate that there's also going to be a series of locations or in array of
locations.
So to indicate that we'll put down locations will then put down a set of square brackets and inside
there we're gonna refer to a separate schema object.
So this separate schema object is going to describe the different objects that are going to be inside
of this array we're going to define that point schema in the same way that we just defined the schema
right here.
We're going to do it in the same file because point schema is only going to make any sense in relation
to our track.
We have to define point schema right above track schema because well we reference that variable right
there right above track schema will define a second schema called point schema
so once again this point schema is going to represent one individual recorded point that we collected
off a mobile device in the world and it's going to have all those different properties U.S. listed right
there.
So we need to define all those different properties inside of our point schema.
So a first take care of timestamp.
So timestamp is going to be a number.
Now once again that might be a little bit surprising usually a timestamp is like a date object where
a string or something like that when we pull a user's location off their mobile device we get the current
time or the time at which that location was pulled inside of a strew a number like a simple long number
that represents the number of milliseconds since 1970.
So we're once again we're just going to take that data and throw it straight into Mongo DV without messing
around with it too much.
There are also going to say this thing has a coords property and the coords property is going to be
an object with a bunch of different properties inside of it so it's going to have a latitude that is
also a number.
We're going to have a longitude that is a number an altitude an accuracy a heading and finally a speed
now.
As usual I have to beg you please double check your spelling on latitude longitude altitude in accuracy
if you make any typo on those property names right there.
They're not gonna be safe properly inside of Mongo DB Okay.
So we've now defined our track schema.
It's going to reference a user.
It's going to have a name and an array of points.
And we defined exactly what a point is inside of a separate schema object right here.
It's not the last thing we're going to do is load up our track schema into Mongoose so down at the bottom
the file we'll call Mongoose dot model with track and track schema.
We're not going to load up the point schema when we call model right here and pass in a schema.
This is the actual step that ties some collection of data inside of Mongo D.B. to Mongoose so we don't
really want to tie point schema with Mongoose because we're not going to have a collection of points
schemas.
Instead all those point objects are embedded so to speak inside of track schema.
So we only have a collection of track objects.
And that's why we are only loading up track into Mongoose OK.
So that's all we have to do to create this representation of a track.
So now we need to start to author another set of routes inside of another file inside of our routes
directory that will allow our mobile device to create a new track or delete a track or view a track
or whatever else we want to do.
So let's start to take care of that in the next video.