So in the last lecture we had a look at the int 32,
let's now clear our persons again and let's now consider an example for a long value.
That's a bit hard to come up with such examples because it's such a very big value you have to store
but let's say we're talking about company values.
So I have my company's collection,
let me quickly clear the screen to make it easier to read.
So I have my company's collection and there, I insert one new company,
again to keep this simple, I'll focus on the one field which is interesting to us here and that's the company
valuation, let's say.
So the company valuation might be a very big number
and if you remember our slide, int 32 integers could only be used for values up to two billion
and something.
Now let's say we have a company which is worth more than that, which is certainly something that you'll
find a lot in today's world,
so we want to store such a big value,
now let me first of all show what will not work.
We could of course try storing a number int because maybe we forgot the maximum range
and now let's say we have a company which is worth 5 billion dollars,
so we need nine zeroes here and if I hit enter here, this gets stored.
Now let me find that company and what we see is that actually a totally different value was stored here,
so valuation is definitely not the value I entered.
The reason for that is we don't get an error here but actually, we entered a too big value and therefore,
it simply stored a different value because it basically exceeded the range of values we had available
and therefore now, we end up with that value.
Why do we get that value and not an error?
Well that has to do with how numbers are handled technically by computers but this is in the end
what we get here, a wrong result.
Now just to demonstrate that my range was correct,
if I take that maximum value you saw in the slide and I insert that, so let's say our valuation is exactly
that biggest value which is 2147483647,
so if I had to get in there and I find all my companies now because I have two in there, you see that value
is exactly the value I stored
but if I only increase it by one digit,
so we have eight instead of a seven at the end
and I find all the companies, now you see
it's a negative value all of a sudden because it basically goes into the negative range here
if we exceed our maximum value here.
So this is what I'm talking about,
this is what I mean, this is what will not work as you might expect if you enter a too big value here and
that's why you have to be aware of the ranges you face.
Now before we have a look at that long value which will help us, let's also try something else.
What if I enter this number,
so the number which was too big here, too big by only one but still too big,
if I enter it like this, so without any special mongodb type and therefore keep in mind, the
shell is based on Javascript,
so we enter this as a 64 bit double.
If I now have a look at all my companies, we see this was actually stored correctly
and the reason for that is that a 64 bit double has a bigger range than a 32 bit int,
so we could use that to store this value but a 64 bit double will still not have the same range of integers
that as the 64 bit integer has.
And the reason for that is obvious,
the 64 bit double doesn't just store integers,
it also has to handle decimal places.
So it's not like the 64 bits are fully available for integer values and therefore if we have a big
integer, the best way to really store it, to be guaranteed that we can store the biggest possible numbers that
are supported by the int 64 range is that we use the number long
wrapper here in the shell or as you saw with the mongodb python example
in the last lecture, if you're using a driver which you will of course in the end, check out which programming
language you are using, what your default values there are
and then in the driver docs, find out which conversion tools, which types, which wrappers, which constructors
you have to create long integers or 64 bit integers and so on.
So here we could create the integer like this,
well we'll face an issue with that but let's have a look at what got stored. Looks correct, right?
Well let me show you what the issue is
if I add more values here. Let's store the biggest possible number we can store in a long value and that
is 9223, whoops 223,
so 9223372036854775807,
now if I try to store that, I get an error
even though I am in the range of accepted values, so I am in the range of values that are supported.
The problem is that this value which is still a number here is simply too big because it's a double
64 which then well gets wrapped by a number long.
So we should wrap this in quotation marks instead
and now it works
and this is really important to understand,
number int and number long can be used with a number passed as a value and with quotation marks
and you should always use quotation marks,
so basically pass a string because mongodb will then internally kind of convert that string
you could say or simply store it appropriately
but if you pass a number, well the number you pass still faces the javascript limitations here in the
shell which is based on Javascript.
So this number which I try to pass originally simply was too big for the shell, for javascript and
therefore couldn't be stored as a number long because we didn't even reach the mongodb handles it
stage. With a string,
a string is just a list of characters,
it's not treated as a number internally and therefore, it doesn't face the limitations we have with numbers.
So we can then store it like this or pass it to number long like this
and then mongodb can take care about it.