So which important number types do we have to differentiate? And these are mostly integers, longs or long
integers and doubles of which we also have different types,
to be precise we can work with these four types of numbers in mongodb. We can have integers, so-called
int 32 integers in mongodb to be precise,
these are full numbers,
so we have no decimal places here. For long,
it's the same, these are also full numbers and I'll talk about the difference between int 64 and int 32 in
a second,
so we get two types of data which are only about storing full numbers. And then we got doubles, also 64
bit which do actually store numbers with decimal places,
so 1.57 could be stored in a double and actually that double, that 64 bit double is the default value
type mongodb uses if you pass a number with no extra information, no matter,
by the way that is important, no matter if that number is theoretically an integer and has no decimal
place or not,
it will be stored as a 64 bit double when passing in the number through the shell.
We also have a special type, high precision doubles in mongodb,
these are also numbers with decimal places
but there's one important difference to the normal doubles,
so let's compare all these types then to find out what the differences are. Int 32 integers
are full numbers in this range, quite a big range and a range that will suffice for a lot of things
you might be doing in your application, since you can store numbers up to 2.1 billion roughly and the
same in the negative range,
you should have plenty of space for a lot of the things you do in a lot of applications.
But sometimes this range is not big enough,
sometimes you simply work with bigger numbers,
for that you can use longs, long integers.
Here you can store way bigger numbers which then definitely should suffice for the types of calculations
or work you might be doing in your application.
If you would store even bigger numbers, you would have to kind of split it up into two fields which you
then manually have to combine in well
however you are processing these numbers. Now
these are the integers
so we're only talking about full numbers with no decimal places here
but of course in a lot of applications, let's say product prices, you have decimal places, your product
might cost 9.99 and not $10,
so for that you got doubles and there, that is important,
the decimal values are approximated
and I will show you what this means in a second.
But it's really important here to understand that for these normal 64 bit doubles, the decimal places
are approximated, not guaranteed or not stored with high precision
and that's exactly the difference to the
well, high precision doubles or decimals as they are also called. There, the decimal values are stored
with high precision
up to 34 decimal digits are stored with high precision,
so you are guaranteed that if you enter 9.9999999, it will be stored as such and not stored differently.
Now you might be wondering why would a double store it with less precision.
Well that's not some bad will by the people who develop mongodb or anything like that,
these are limitations you face in computing due to the way numbers are stored or how computing works
in general with bits and so on.
Now diving deeper into this would be way beyond the scope of this course as it is a highly theoretical
and mathematical topic
but in the last lecture of this module, you will find some links to discussions that might be interesting
to you if you want to dive into all the theory here and want to learn way more.
For now we just take it as granted,
doubles for some technical reasons are not guaranteed, so the decimal places there are not guaranteed to be
correct,
they are approximated, for the decimals so the 128 bit doubles,
you have a guarantee for up to 34 decimal digits.
The way this works is that essentially these 128 bit decimals have a lower range,
so the part before the comma is lower therefore you have more precision after the comma, for doubles,
you have a higher range,
so the integer part is bigger but the increased size there is basically one by losing precision
after the comma you could say. So this is the rough overview and we'll see all that in the shell
and in practice in a second. As a first quick summary, the idea of course is that you use integers
here for normal integers, if you know you store some value which is a number in that range here
and which only is an integer, consider using an integer and you should consider using that because it will
simply take up less space than if you just enter it as a normal value and therefore it automatically
gets stored as a 64 bit double. Use a long if you need it, if your range for int 32 integers is not enough,
use it a long if you only work with full numbers.
You can use doubles for basically all values where you don't need high precision you could say,
so this is your quick and lazy approach to storing numbers
but it's also a valid approach for storing numbers that have digits, that have decimal places where you
just don't need high precision.
And we of course have the high precision doubles here
if you need that high precision for calculations with monetary data, let's say or for scientific calculations.
You will have noted that throughout the course,
we always just used doubles, so just entered a value and therefore, we automatically used the 64 bit doubles
here
and that was simply also to not focus on the numbers too much there and simply get the values into the
database,
focus on the different parts we explained like the operators, how to query stuff and so on.
If you're building an application and you know you're storing something like an age, you should at least
consider taking an int 32 instead of the lazy route of using a double because it will be smaller but more
on that in the next lectures
now.