In the last couple of videos we've spent our time inserting documents into the database right here as
an example.
This code inserts three documents into the tasks collection each task has a description and completed
field.
Now when we viewed that data over in robo 380 we saw all of our descriptions and all of our completed
values along with one additional field which we did not specify underscore I.D..
Now I briefly mentioned that this field is automatically created by Mongo D.B. and it stores a unique
identifier for each document you insert into the database.
In this video before we go any further I want to take a little time to talk about the value inside.
We're gonna talk about what object ideas are why they're used and how they're going to play an important
role in Mongo D.B..
Now the value we're seeing here for the I.D. is much different than what we would see in a traditional
Eskew l database.
And this is by design.
So in an essay you old database like my ask you l these server generates the I.D. for new records and
it follows that auto incrementing integer pattern where the first record has the idea of one.
The second two three four and so on with Mongo D.B. the ideas are known as G you I.D. which stands for
globally unique identifiers and these are a bit different.
They're designed to be unique using an algorithm without needing the server to determine what the next
I.D. value is switching from auto incrementing integers over to globally unique identifiers allowed
Mongo D.B. to achieve one of its main goals which is the ability to scale well in a distributed system.
So we have multiple database servers running instead of just one allowing us to handle heavy traffic
where we have a lot of queries coming in with Mongo D.B. and new ideas.
There's no chance of an IED collision across those database servers with an auto incrementing integer.
It's definitely possible that we could have a user with an I.D. of one in one database server and a
user with an I.D. of one in another database server and we could eventually run into an issue where
those ideas conflict with Mongo D.B. we don't run into that problem.
Another great advantage is that we can actually generate the ideas for our documents before we ever
insert them into the database.
So our server doesn't need to be the one who generates the I.D. we can actually use that Mongo D.B.
library to generate object ideas of our own.
And that's where I want to start.
We're gonna generate some object I.D. using node j s then we'll dissect them and explore exactly what
they're made of.
Let's switch over to visual studio code and get this process started.
The first thing I'm gonna do is comment out our call to insert many down below where we insert into
the tasks collection.
The reason I'm commenting this out is that I don't want to insert these same tasks again when the file
runs for the moment.
We're not actually going to change the database in any way.
What we're gonna do is grab something else off of Mongo D.B. B that allows us to work with object I.D.
So down below where we set up Mongo client.
I can create a new constant for a capital O object capital I capital D.
And we're going to get the value for that off of a property on mongo D.B. with the exact same name object
I.D..
Now you'll notice here that we're grabbing a lot of individual things off of Mongo D.B. and we're storing
those and individual variables.
This is a great use case for disk structuring which we covered earlier in the class.
So as an example we could achieve the exact same thing with the following code.
I'm going to comment out this code so we can leave it in place and what we're going to do is create
a constant.
Now we know we get an object back from require Mongo D.B. and we know it's an object because we've been
accessing properties on it.
So right here we'll just de structure that I'm going to grab Mongo client and I'm also going to grab
object I.D..
Now where am I going to grab them from an object.
That's how d structuring works and I have the object as the return value for calling require with Mongo
d be perfect.
So this is just a shorthand for grabbing things off of Mongo D.B..
Now the old code is still going to work exactly the same Mongo client and that connect is still going
to work as expected.
But now we're gonna be able to generate our own ideas right here from the file and to do that.
Let's start by creating a variable to store the idea.
Then we're going to use object I.D. which we just grabbed.
Now that's a constructor function.
So we use it with the new keyword that is new object capital I capital D.
And we call it providing no arguments.
This is a function that's going to generate a new for us now the new keyword is technically optional
because the Mongo DB library has a little defensive programming built in to add it.
If you don't but it's in general a good idea to add it in ourselves down below.
We can now do something with this I.D. and all I'm going to do is dump it to the console.
So for me console dialogue printing I.D. to the terminal.
Now that we have this in place let's go ahead and test things out down below.
We are running commands from the task manager directory and we're just going to run this file once again
right here node space Mongo D.B. Deighton J.
S and when I run it what do I get.
I get a brand new object I.D. showing up.
Now you'll notice that once again the program is hanging because we do have that open connection.
We can always use control C to shut that down.
Now let's talk about the I.D. value itself.
What we have here is not just one long randomly generated series of characters.
There's actually a few different pieces of information inside of here embedded in there for example
is a timestamp and we can learn more by heading over to the browser.
Now you don't need to pull up the following web site since we're only going to use it for a brief moment
as a visualization to see what I'm talking about.
I'm going to Google Mongo D.B. object I.D. and we're looking for their object I.D. documentation on
the reference site.
So right here that is the reference documentation as opposed to the API docs.
And when we pull that up it's gonna give us a breakdown of what exactly an object I.D. is made of right
here and object I.D. is a 12 byte value which consists of the following the first four bytes of the
object I.D. represent the number of seconds since the Unix epic.
So that is a point in time which has been decided upon and that is midnight January 1st of 1970.
So embedded inside of all of your object I.D. is actually a timestamp which knows when the particular
object I.D. was created which can be of great value in the long term as your dataset grows.
Next up after the first four bytes we have the following five bytes.
These are a randomly generated value.
And the last three bytes are a counter starting with a random value.
Now when we put all this together we have a very complex algorithm that allows us to generate a globally
unique I.D. without needing a central authority to tell us what the next I.D. is.
And we just saw that since we generated an object I.D. inside of our node j s code.
Now I'm going to close down this tab and we're going to head back over to the API documentation down
below under O for object I.D..
There is indeed an entry.
And if we click that we can explore some of the methods available to us.
And the only one we're really going to use and we're only going to use it for this example is get timestamp
get timestamp gets the timestamp that's stored inside of the first four bytes for that object I.D. and
we can go ahead and use this method to actually print those out.
So right here we're going to add another console dot log line down below console dot log I.D. dot get
timestamp and we are going to call that as a function which it is and the documentation says it doesn't
need to take any arguments and we're going to get our timestamp back.
So what I'm going to do is save the program and rerun it to view the timestamp right here I'll shut
things down and rerun the script.
And what do I get.
I get a timestamp right here I can see the current point in time which is indeed accurate.
And right here I have the exact hour minute and second and if I shut the script down and I rerun it
you can see it's updated a little bit.
We went from the forty ninth minute to still the forty ninth minute but we were at thirty four seconds
into the minute earlier and the second time I ran it.
We were at forty seven seconds.
So as we generate new ideas either on our own or as Mongo D.B. generates them for us a timestamp is
indeed embedded right inside of there allowing you to know when it was created.
Now let's go ahead and use our generated object I.D. as the actual I.D. for a new document.
We insert so down below I have the first insert one call we explored a couple of videos ago.
I'm gonna take that entire call and uncommon to it and we'll just change up the data a little bit.
Right here I have the name.
I'll change that over to Vikram and I'll change the age as well to something like twenty six.
Now alongside of the two fields we're also going to specify underscore I.D..
And we have to provide if we're choosing to set underscore I.D. an object I.D. we already have one created
and stored in the I.D. variable so I can go ahead and reference that right here.
Now let's go and save the program to rerun things when we rerun it.
We're gonna see our new I.D. printed to the terminal and then we're gonna be able to compare that with
the I.D. we see over in rowboat 3 t when we view our document.
So right here I'll shut it down.
I'll start it up again.
And we have our data showing up.
So we have the actual data that came back from the server as the result of the operation and it matches
up perfectly with the I.D. we have right here.
If we had over two robo three T we'll be able to see the exact same value in that user's collection.
I'm going to refresh the page using command or control are and right here we do indeed have the exact
same I.D. that we had just generated over inside of the terminal.
Now there's just one more thing I want to talk about related to object I.D. before we wrap up for the
moment.
I'm going to remove that I.D. property and I'm going to comment out in this example once again I just
set that up to show you that you can provide your own object I.D. If you want to.
Now for our purposes we're not going to do that because it's just extra code we can have Mongo D.B.
automatically generate it for us.
And in most cases we don't need to know the object I.D. value before the document is inserted.
But if you needed to you could write here the last thing we're going to talk about is how object I.D.
are stored because that plays an important role as well in the terminal when we see an object I.D. we
just see these series of characters that make it up.
So right here we have an I.D. that starts with five C and ends with 7 1.
Now over in robo 3 T we have the exact same I.D. that starts with five C and ends with seven one for
the new document we just created.
But there's also some extra stuff on each end.
What we have here is a function call where this string is being provided as the first and only argument.
So this is a visualization making it easier to see the object I.D. value because I.D. are indeed a binary
data.
Now the reason they're using binary data over a traditional string has to do with the size of each.
By using binary instead of a string they're able to cut the size of an object I.D. in half and we can
quickly prove that.
Just to explain exactly why this is the way they chose to go about things.
So over inside of our code we have this I.D. variable that actually has an I.D. property on it.
This contains the raw binary information.
But I'm going to do is take a quick moment to save the file and rerun it just so we can see that I'll
shut things down.
Start them up again.
And right here we have our buffer with the binary data inside.
Now we can check the length of this by using I.D. dot I.D. dot length and right here we're going to
see if we rerun the program that the length is indeed at 12:00 which matches up exactly with what we
explored in the documentation before we have that 12 byte value where we have the timestamp the random
value and we have the incrementing value at the end.
Now if we were to convert that from binary into a string it would double in size.
So right here we can do just that using a method available on object I.D. if we head over to the API
docs for one final time and we already explore to get timestamp to do what we want to do.
We're gonna use two hex string which is going to convert it over to its string representation.
So right here to get that done we'll use console dot log I.D. dot to hex string we're gonna call that
as a function that's going to return the string representation and we're gonna do is check its length.
So right here we're printing the original object I.D. length then the length of the string representation
I'm going to save the program I'm going to shut things down I'm gonna restart it and right here we can
see that the original representation was just 12 while the string representation was double in size
at twenty four.
So this is the reason we are seeing object I.D. all around it is just a way to visualize easier for
humans.
What exactly is happening.
So here it's giving us the representation as a string but it's wrapping it in this object I.D. call
to let us know that it's not actually a string it's what comes back from this function call which would
be binary data but to save us a lot of time and to save us from some really long lines in our table
all of that is abstracted away from us the end user of Mongo D.B..
Now you might be wondering why did we just spend so much time learning about ideas we were just getting
into the fun stuff and the reason is that we need to know about object I.D. in order to be able to fetch
our documents by I.D. which is something we're going to do in the very next video.
So that's it for this one and there's no challenge here.
I just wanted to give you a little background on what exactly is going on with I.D. in Mongo D.B. in
the next video.
We'll get back to exploring the database and you'll learn how to fetch your documents out of it.
Let's go ahead and jump right in.