We had a look at number int and number long.
Now one obvious question you might have at this point is
especially with that string thing which I mentioned at the end of the last lecture,
why don't we just store all numbers as strings?
Let's say we have accounts collection with bank accounts or something like this and in there, we could
insert one bank account for one person,
so for person Max, of course we don't need that name and the interesting part here is like what's in
there, so amount and now we could store it as a value like this but let's say we only have integer values
there, unrealistic of course but let's say we only have full numbers, so we could of course store it
as a number
or number long depending on how big the number will be,
I can tell you number int would suffice for me here
but why don't we just store it as text
if we face no problem here and we can store as big of a number as we want like this, which is not my
actual bank account but let's say this could be stored,
it's a valid text, the text can be as long as you want,
so why don't we store it like this? If I now have a look at my bank accounts here, we see that value was
of course stored exactly because it's just some text.
Now let's delete this quickly
and insert this again a bit smaller
so that we that we can see what's going on and let's just store 10.
Now one reason why storing it as a text is a bad idea is
because if I now need to do any calculations with it, it will just fail.
So let's say I want to update my account here and I only have one entry in here, so I don't need to
filter and update one and I do this by incrementing, you learned about the inc operator, by incrementing
amount by 10. If I hit enter here,
we see that we cannot apply inc to a value of non-numeric type,
so it essentially fails because amount is a string and not a number.
And that is one problem we'll face if we ever want to do calculations, we'll not work with a string,
it will work with number int and number long though.
So if I clear my account again
and I now insert my account, my amount but not as a string
but let's say as number int, 10 wrapped in quotation marks which is the preferred way of passing a value
here,
if I do that and I repeat my update one operation, now this succeeded.
And now there's one important thing,
if I now output this, we see it's 20 but now it's not a number int anymore,
it's a normal 64 bit float
again. The reason for that is that I increment it by such a normal number and not by an int and therefore
mongodb automatically converts it to this type of number,
so to the float 64.
So if I wanted to keep it a number int, I'll quickly delete everything, re-insert my initial 10 bucks
into my account and then update, I would update by also wrapping this with number int,
so I would update by passing a number int
and this as you see succeeds.
So we're not working with strings here,
we are working with these special number types provided by mongodb
and therefore now, if I have to look at this, we see the amount 20.
Now unfortunately this still looks like a normal 64 bit because it is received to automatically display
that such by the shell here
but it will actually be stored as a number int
behind the scenes because we only work with number ints for both inserting the document and updating
it. Now the same does work for longs,
if we go back to the companies example and I first of all, delete everything in there so that we can start
from scratch,
let me then insert one document into companies.
Let's say there we have the funding or not the funding, let's say the evaluation and let's add number
long here.
Now let's enter a big number,
like this which is too big for int 32 but which is ok as a number long. Let's first of all look into our
companies here,
so companies find one, this is the company I entered, the value was stored in there, it was stored in exactly
the value I added. By the way, just because I haven't mentioned it yet but of course you can also store
negative numbers,
so I'm using positive numbers here but the same is possible for the same range as you saw in the slide
with negative numbers.
So I store this
and now let's update this to show that we again can do calculations, mathematical calculations on number
longs too. If I update one document, the only one I have in there, I can do this with the inc
operator and I want to update my valuation by let's say, let's first of all say 100 or let's say only
one because that will be easier to check,
so by one. It succeeds
but if I find my value, we actually see we got a problem now. The value we get back is incorrect
and the reason for that is simply by changing it by one, this was converted to a 64 bit float which can
store a number this big
and therefore simply well rounds it in the end.
So this is of course not what we wanted,
so let's reset this,
delete everything,
insert our value with the number long
and now update again
but now I update with number long and that will be my value of one
but now again, created as a long number
with that special constructor provided by the mongodb shell. This succeeds
and if I now have a look at my element here, we see this is indeed the correct number
now, previously we had 89 at the end,
now it's 90 because we incremented by 1.
So this is how we can do math with these number longs and number ints
and that is of course a crucial part of using them, so you can use them.
You can also filter by them by the way, you can use them with greater than filters and sort by them,
so they really work like normal numbers, just with the benefits of well this enhanced range or this bigger
range you have available.