1 00:00:02,520 --> 00:00:04,960 We had a look at number int and number long. 2 00:00:05,220 --> 00:00:08,500 Now one obvious question you might have at this point is 3 00:00:08,880 --> 00:00:12,970 especially with that string thing which I mentioned at the end of the last lecture, 4 00:00:13,050 --> 00:00:16,710 why don't we just store all numbers as strings? 5 00:00:16,740 --> 00:00:24,990 Let's say we have accounts collection with bank accounts or something like this and in there, we could 6 00:00:25,050 --> 00:00:27,650 insert one bank account for one person, 7 00:00:27,780 --> 00:00:33,510 so for person Max, of course we don't need that name and the interesting part here is like what's in 8 00:00:33,510 --> 00:00:39,440 there, so amount and now we could store it as a value like this but let's say we only have integer values 9 00:00:39,530 --> 00:00:44,370 there, unrealistic of course but let's say we only have full numbers, so we could of course store it 10 00:00:44,460 --> 00:00:46,070 as a number 11 00:00:46,320 --> 00:00:48,950 or number long depending on how big the number will be, 12 00:00:48,960 --> 00:00:51,620 I can tell you number int would suffice for me here 13 00:00:51,780 --> 00:00:55,020 but why don't we just store it as text 14 00:00:55,140 --> 00:01:01,050 if we face no problem here and we can store as big of a number as we want like this, which is not my 15 00:01:01,050 --> 00:01:04,740 actual bank account but let's say this could be stored, 16 00:01:04,820 --> 00:01:07,650 it's a valid text, the text can be as long as you want, 17 00:01:07,650 --> 00:01:17,150 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 18 00:01:17,150 --> 00:01:21,130 of course stored exactly because it's just some text. 19 00:01:21,350 --> 00:01:23,690 Now let's delete this quickly 20 00:01:31,010 --> 00:01:32,800 and insert this again a bit smaller 21 00:01:35,230 --> 00:01:39,490 so that we that we can see what's going on and let's just store 10. 22 00:01:39,640 --> 00:01:42,760 Now one reason why storing it as a text is a bad idea is 23 00:01:42,790 --> 00:01:48,440 because if I now need to do any calculations with it, it will just fail. 24 00:01:48,700 --> 00:01:55,800 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 25 00:01:55,800 --> 00:02:04,320 filter and update one and I do this by incrementing, you learned about the inc operator, by incrementing 26 00:02:04,790 --> 00:02:09,940 amount by 10. If I hit enter here, 27 00:02:09,950 --> 00:02:14,430 we see that we cannot apply inc to a value of non-numeric type, 28 00:02:14,570 --> 00:02:20,360 so it essentially fails because amount is a string and not a number. 29 00:02:20,360 --> 00:02:26,920 And that is one problem we'll face if we ever want to do calculations, we'll not work with a string, 30 00:02:27,140 --> 00:02:31,250 it will work with number int and number long though. 31 00:02:31,340 --> 00:02:33,800 So if I clear my account again 32 00:02:34,100 --> 00:02:38,530 and I now insert my account, my amount but not as a string 33 00:02:38,600 --> 00:02:45,770 but let's say as number int, 10 wrapped in quotation marks which is the preferred way of passing a value 34 00:02:45,770 --> 00:02:46,500 here, 35 00:02:46,550 --> 00:02:52,570 if I do that and I repeat my update one operation, now this succeeded. 36 00:02:52,760 --> 00:02:54,290 And now there's one important thing, 37 00:02:54,390 --> 00:02:58,500 if I now output this, we see it's 20 but now it's not a number int anymore, 38 00:02:58,550 --> 00:03:01,660 it's a normal 64 bit float 39 00:03:01,670 --> 00:03:07,940 again. The reason for that is that I increment it by such a normal number and not by an int and therefore 40 00:03:08,030 --> 00:03:11,300 mongodb automatically converts it to this type of number, 41 00:03:11,300 --> 00:03:14,610 so to the float 64. 42 00:03:14,650 --> 00:03:23,140 So if I wanted to keep it a number int, I'll quickly delete everything, re-insert my initial 10 bucks 43 00:03:23,170 --> 00:03:29,570 into my account and then update, I would update by also wrapping this with number int, 44 00:03:29,620 --> 00:03:31,930 so I would update by passing a number int 45 00:03:32,050 --> 00:03:33,900 and this as you see succeeds. 46 00:03:33,910 --> 00:03:35,700 So we're not working with strings here, 47 00:03:35,710 --> 00:03:40,360 we are working with these special number types provided by mongodb 48 00:03:40,470 --> 00:03:44,370 and therefore now, if I have to look at this, we see the amount 20. 49 00:03:44,500 --> 00:03:52,870 Now unfortunately this still looks like a normal 64 bit because it is received to automatically display 50 00:03:52,870 --> 00:03:54,450 that such by the shell here 51 00:03:54,520 --> 00:03:57,150 but it will actually be stored as a number int 52 00:03:57,140 --> 00:04:04,120 behind the scenes because we only work with number ints for both inserting the document and updating 53 00:04:04,120 --> 00:04:08,650 it. Now the same does work for longs, 54 00:04:08,780 --> 00:04:16,340 if we go back to the companies example and I first of all, delete everything in there so that we can start 55 00:04:16,340 --> 00:04:17,770 from scratch, 56 00:04:17,840 --> 00:04:22,280 let me then insert one document into companies. 57 00:04:25,270 --> 00:04:31,260 Let's say there we have the funding or not the funding, let's say the evaluation and let's add number 58 00:04:31,260 --> 00:04:32,380 long here. 59 00:04:32,410 --> 00:04:34,580 Now let's enter a big number, 60 00:04:36,680 --> 00:04:44,750 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 61 00:04:44,750 --> 00:04:45,560 companies here, 62 00:04:45,560 --> 00:04:52,670 so companies find one, this is the company I entered, the value was stored in there, it was stored in exactly 63 00:04:53,180 --> 00:05:00,030 the value I added. By the way, just because I haven't mentioned it yet but of course you can also store 64 00:05:00,030 --> 00:05:00,870 negative numbers, 65 00:05:00,870 --> 00:05:05,190 so I'm using positive numbers here but the same is possible for the same range as you saw in the slide 66 00:05:05,280 --> 00:05:06,490 with negative numbers. 67 00:05:06,840 --> 00:05:08,530 So I store this 68 00:05:08,700 --> 00:05:15,430 and now let's update this to show that we again can do calculations, mathematical calculations on number 69 00:05:15,450 --> 00:05:21,390 longs too. If I update one document, the only one I have in there, I can do this with the inc 70 00:05:21,410 --> 00:05:30,670 operator and I want to update my valuation by let's say, let's first of all say 100 or let's say only 71 00:05:30,670 --> 00:05:32,670 one because that will be easier to check, 72 00:05:32,680 --> 00:05:34,770 so by one. It succeeds 73 00:05:34,780 --> 00:05:41,060 but if I find my value, we actually see we got a problem now. The value we get back is incorrect 74 00:05:41,080 --> 00:05:49,030 and the reason for that is simply by changing it by one, this was converted to a 64 bit float which can 75 00:05:49,030 --> 00:05:50,410 store a number this big 76 00:05:50,470 --> 00:05:53,440 and therefore simply well rounds it in the end. 77 00:05:53,440 --> 00:05:55,840 So this is of course not what we wanted, 78 00:05:57,060 --> 00:05:59,770 so let's reset this, 79 00:05:59,790 --> 00:06:01,100 delete everything, 80 00:06:01,140 --> 00:06:03,180 insert our value with the number long 81 00:06:03,420 --> 00:06:04,730 and now update again 82 00:06:04,740 --> 00:06:10,130 but now I update with number long and that will be my value of one 83 00:06:10,320 --> 00:06:13,970 but now again, created as a long number 84 00:06:14,070 --> 00:06:19,740 with that special constructor provided by the mongodb shell. This succeeds 85 00:06:19,740 --> 00:06:26,350 and if I now have a look at my element here, we see this is indeed the correct number 86 00:06:26,340 --> 00:06:28,770 now, previously we had 89 at the end, 87 00:06:28,770 --> 00:06:31,540 now it's 90 because we incremented by 1. 88 00:06:31,830 --> 00:06:36,160 So this is how we can do math with these number longs and number ints 89 00:06:36,270 --> 00:06:40,210 and that is of course a crucial part of using them, so you can use them. 90 00:06:40,230 --> 00:06:45,280 You can also filter by them by the way, you can use them with greater than filters and sort by them, 91 00:06:45,360 --> 00:06:53,850 so they really work like normal numbers, just with the benefits of well this enhanced range or this bigger 92 00:06:53,850 --> 00:06:55,280 range you have available.