Ok
so let's now play around with these data types a bit,
I'm on my MongoDB server again and I dropped all existing databases,
you can by the way always do that by switching to the database like test if I had that database here and
then executing
drop database, db.dropdatabase and this will get rid of the database
if you have it. But let me now clear the screen
and let's now use a new database,
companies or company data maybe
and in there, let's say I have a company's collection and in that company's collection, I now want to insert one new
company and my idea here is that for this company, I want to showcase some of these data types. So that
company will probably have a name and that name will be some text.
Now as you learned, the text is enclosed in quotation marks and you can by the way use double or single
quotation marks here,
this does not matter,
that is the case in the shell here because the shell is based on javascript, for the programming language
you're using with the MongoDB driver, that might differ of course.
So here, let's give this any name, like Fresh Apples Inc, it's
a company I just came up with and now with that company name added here,
what else could we add?
Well let's say this company is a startup and therefore we add a isStartup key here and this simply is well
true,
so here I'm using a boolean because the case or the question whether this is a startup or not can be
answered with yes or no
and translated to a boolean, that would be true or false,
here it is yes,
so it's true.
Now besides that being a startup, let's also log its number of employees and I'll add 33 here.
So this is a number and this is actually an integer you would think at least, now the shell which
I'm using here is based on javascript and javascript doesn't differentiate between integers and floating
point numbers,
this would be a floating point number by the way.
So this will actually be stored as a double or float in MongoDB, not as this number decimal special
high precision thing I mentioned on the slide but a normal floating point number with some imprecision
on the decimal places.
Now that might be fine
and for now, let's simply store it like that,
let's also add a funding here and let's store a very large number there and I'll actually store a number
which is easy to remember by just sliding over my numbers twice,
so it's 1234567890 and then again, let's also add an embedded document here, details
maybe and there we could simply have any additional data we want like CEO Mark Super,
obviously a good name for a CEO
and let's also add a list, tax maybe. That is an array, you always add an array by using square brackets
and there in turn, we can add any value we want,
so this can hold multiple string values like super perfect, whatever you want,
it could also hold numbers and all the values in the array are just separated by commas
or it could of course also hold other nested documents, so this could hold other documents with tax which
are well basically documents with a title that then holds the value,
so whatever your application needs is possible, you're really flexible here.
So here I go for the embedded document in an array style.
Now besides these values, we also learned about dates and timestamps,
so if we also add the foundingdate here, we can construct a new date in the shell by calling new date
and if we add it like this, it will take the current date and I will indeed do that here.
We can also add a timestamp like insertedAt to give that a clear timestamp when this data was
inserted by calling new timestamp.
Now these are functions, new date and new timestamp that are provided by the shell,
there are equivalents in the drivers for the different programming languages though and you'll find
the exact methods you need to call in the driver docs.
Here for example, I'm on the page of the MongoDB Nodejs driver
and there I can click on the reference of the latest release
and of course such release references are available for all drivers and in the documentation for the
driver,
you find detailed instructions on installation and how to use it and you should also find an API
documentation which is simply a list of all the features that are included and in there for example
you also find long and int32 that will be useful methods or objects when working with these values
and you also will find something that helps you with a timestep for example.
So down there, if you click on timestamp, you open the timestampe doc and you learned how you can create
a timestamp in that NodeJS driver,
so there by calling new timestamp after importing the timestamp from this mongodb driver.
And more information on that and how to use that driver and so on can
as I mentioned before we found in the general driver docs, like the tutorials here, the quickStart or the
general reference here, so that is how you can use these times you learned about here in a shell in
your driver too.
So the shell here is based on Javascript, therefore here we actually do use new timestamp to create a
timestamp
and with that for now I added all the data I want to add.
If I now hit enter, this gets inserted
and now let's have a look at it by finding one company,
so the one we just added. And here we see that company was added,
we see the data we added,
we see for example for the date, we got this ISO date object with the current date
I'm recording this at as a value entered here.
You see it uses the year year year, month month, day day format then a T to separate the time from the
date and then we got the time in a 24 hour format stored here,
the set at the end marks. the timezone for which this time is logged.
You also see that timestep object
and as I mentioned this is actually a timestamp based on the current time in milliseconds
and also it has an ordinal component so that if we were to insert two elements simultaneously with
insertMany, they would have different timestamps.
Now one super interesting thing can be seen if we have a look at funding,
there you see that the number that is stored is actually not the number that I added,
if I scroll up a bit, you see that for funding I did add 1234567890 twice essentially,
so I rolled over the keyboard twice but the number that was stored is 123456789012
34567 and then it's essentially nullified or cut off
you could say. The reason for that is that I tried to store a number that was too big because the normal
number javascript accepts is essentially a 64bit floating point value
and that could not be stored here
and that is a restriction you can face when working with numbers.
If you need to store super big numbers, you'll have to store them separately, maybe as a string.
Now speaking of numbers, you saw some general values here on the company,
now let me get a little bit more abstract so that I can show you what's up with these numbers and I
have a whole module where I also talk about numbers and how to work with number data
but for now, let me quickly add some dummy data. I'll use a new collection for this, numbers and this will
be one of the rare cases where I'll just use an abstract example. I'll insert one document here, a 1,
now if I insert it like this, obviously if I access numbers and I find one element, we see it here.
Now one interesting thing can be seen if I now use db.stats, stats is a utility method provided by
the shell which outputs some well, stats about this database.
Here we see like how many collections we got in there, how many objects
and we also see the average object size.
Now I got two collections in there,
so let me quickly get rid of that company's collection by calling drop on the collection, like drop database
gets rid of a database, drop on a collection gets rid of that database.
If I now repeat stats, we see there's only one collection there and we get a data size, an average data size
of 33,
now remember that value of 33 here. Now I will use my numbers collection and delete all elements,
so the one element I got in there, if I now execute db.stats,
we obviously see data size zero because there's nothing in there.
Now let me repeat that
insert one command here but I'll not insert a number like this, which remember is stored as a 64bit
floating point number by default but I'll use a special method provided by the shell and you'll find
equivalence in your driver docs when working with a driver which will create an int32 value instead
of that default 64 bit
floating point number
and if I store the same number one, now with this helper function and I repeat my stats output, you'll
see that it shrank a bit
and this is one of the reasons why you could consider using these special values provided by mongodb
because you're able to manage your data size a bit more efficiently.
Now it's also worth pointing out that this was only stored as this 64bit floating point number because
the shell is based on javascript and javascript doesn't differentiate between integers and floats,
other languages do
so for other languages,
this might not be the case if you accessed your database through the driver and added a number there, for
the shell
it is however and this is good because here I can show you the difference between that default number,
the float and this integer.
You can also check the type of a value as it is stored in mongodb by using the type of command which
is provided in the shell. There
I can access numbers,
find one to get that one element I have and then access the a key with .a and we see the type here is
number.
Now I don't want to go too in-depth into the whole number thing,
also because it has a lot to do with computing in general and not so much with mongodb but I've found
this important to highlight that you have these different formats.
Often it might not matter and the default which is stored might work for you
like for all the numbers we used thus far for ages or prices, we can just enter the number and it will get
stored in the database,
the sizing difference might not really matter for us,
the extra precision we could get on the decimal places will also not matter for only two or three decimal
places,
so there we will be fine with the defaults,
I want you to be aware of the general set of data types you have and that there are specialized data
types for specialized kind of operations
you might plan.
You should also not miss the bson types document to which you find a link in the last lecture of this module
where you can learn more about the general data types
mongodb knows and how it can store data in its database and where you can then also learn more
about some of the special data types like objectid or timestamps and which in general is a great backup
if you want to dive deeper into a specific data type or learn more about a specific data type.