1 00:00:02,290 --> 00:00:05,050 So in the last lecture we had a look at the int 32, 2 00:00:05,140 --> 00:00:11,780 let's now clear our persons again and let's now consider an example for a long value. 3 00:00:11,860 --> 00:00:16,560 That's a bit hard to come up with such examples because it's such a very big value you have to store 4 00:00:16,780 --> 00:00:21,650 but let's say we're talking about company values. 5 00:00:21,800 --> 00:00:24,020 So I have my company's collection, 6 00:00:24,020 --> 00:00:27,230 let me quickly clear the screen to make it easier to read. 7 00:00:27,440 --> 00:00:32,060 So I have my company's collection and there, I insert one new company, 8 00:00:34,740 --> 00:00:39,530 again to keep this simple, I'll focus on the one field which is interesting to us here and that's the company 9 00:00:39,530 --> 00:00:41,120 valuation, let's say. 10 00:00:41,180 --> 00:00:43,880 So the company valuation might be a very big number 11 00:00:44,210 --> 00:00:52,370 and if you remember our slide, int 32 integers could only be used for values up to two billion 12 00:00:52,460 --> 00:00:53,780 and something. 13 00:00:53,780 --> 00:00:58,550 Now let's say we have a company which is worth more than that, which is certainly something that you'll 14 00:00:58,550 --> 00:01:00,970 find a lot in today's world, 15 00:01:00,980 --> 00:01:02,960 so we want to store such a big value, 16 00:01:02,960 --> 00:01:06,520 now let me first of all show what will not work. 17 00:01:06,520 --> 00:01:10,970 We could of course try storing a number int because maybe we forgot the maximum range 18 00:01:11,030 --> 00:01:15,590 and now let's say we have a company which is worth 5 billion dollars, 19 00:01:15,650 --> 00:01:23,330 so we need nine zeroes here and if I hit enter here, this gets stored. 20 00:01:23,350 --> 00:01:31,750 Now let me find that company and what we see is that actually a totally different value was stored here, 21 00:01:31,840 --> 00:01:35,340 so valuation is definitely not the value I entered. 22 00:01:35,470 --> 00:01:42,670 The reason for that is we don't get an error here but actually, we entered a too big value and therefore, 23 00:01:43,530 --> 00:01:50,080 it simply stored a different value because it basically exceeded the range of values we had available 24 00:01:50,380 --> 00:01:52,730 and therefore now, we end up with that value. 25 00:01:52,750 --> 00:01:54,960 Why do we get that value and not an error? 26 00:01:55,030 --> 00:02:01,150 Well that has to do with how numbers are handled technically by computers but this is in the end 27 00:02:01,150 --> 00:02:03,370 what we get here, a wrong result. 28 00:02:03,430 --> 00:02:06,520 Now just to demonstrate that my range was correct, 29 00:02:06,730 --> 00:02:14,440 if I take that maximum value you saw in the slide and I insert that, so let's say our valuation is exactly 30 00:02:14,440 --> 00:02:21,810 that biggest value which is 2147483647, 31 00:02:21,910 --> 00:02:30,880 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 32 00:02:30,890 --> 00:02:32,730 is exactly the value I stored 33 00:02:32,730 --> 00:02:35,130 but if I only increase it by one digit, 34 00:02:35,130 --> 00:02:37,860 so we have eight instead of a seven at the end 35 00:02:38,370 --> 00:02:40,270 and I find all the companies, now you see 36 00:02:40,320 --> 00:02:44,990 it's a negative value all of a sudden because it basically goes into the negative range here 37 00:02:45,060 --> 00:02:47,400 if we exceed our maximum value here. 38 00:02:47,430 --> 00:02:48,750 So this is what I'm talking about, 39 00:02:48,750 --> 00:02:54,720 this is what I mean, this is what will not work as you might expect if you enter a too big value here and 40 00:02:54,720 --> 00:02:57,700 that's why you have to be aware of the ranges you face. 41 00:02:57,780 --> 00:03:03,350 Now before we have a look at that long value which will help us, let's also try something else. 42 00:03:03,570 --> 00:03:05,290 What if I enter this number, 43 00:03:05,340 --> 00:03:10,660 so the number which was too big here, too big by only one but still too big, 44 00:03:10,770 --> 00:03:16,350 if I enter it like this, so without any special mongodb type and therefore keep in mind, the 45 00:03:16,350 --> 00:03:17,860 shell is based on Javascript, 46 00:03:17,880 --> 00:03:21,060 so we enter this as a 64 bit double. 47 00:03:21,390 --> 00:03:25,950 If I now have a look at all my companies, we see this was actually stored correctly 48 00:03:25,950 --> 00:03:32,130 and the reason for that is that a 64 bit double has a bigger range than a 32 bit int, 49 00:03:32,340 --> 00:03:40,650 so we could use that to store this value but a 64 bit double will still not have the same range of integers 50 00:03:40,650 --> 00:03:45,100 that as the 64 bit integer has. 51 00:03:45,120 --> 00:03:46,450 And the reason for that is obvious, 52 00:03:46,500 --> 00:03:49,560 the 64 bit double doesn't just store integers, 53 00:03:49,650 --> 00:03:51,780 it also has to handle decimal places. 54 00:03:51,870 --> 00:03:57,510 So it's not like the 64 bits are fully available for integer values and therefore if we have a big 55 00:03:57,510 --> 00:04:04,470 integer, the best way to really store it, to be guaranteed that we can store the biggest possible numbers that 56 00:04:04,530 --> 00:04:09,150 are supported by the int 64 range is that we use the number long 57 00:04:12,210 --> 00:04:17,550 wrapper here in the shell or as you saw with the mongodb python example 58 00:04:17,550 --> 00:04:23,040 in the last lecture, if you're using a driver which you will of course in the end, check out which programming 59 00:04:23,040 --> 00:04:26,450 language you are using, what your default values there are 60 00:04:26,730 --> 00:04:33,090 and then in the driver docs, find out which conversion tools, which types, which wrappers, which constructors 61 00:04:33,090 --> 00:04:38,850 you have to create long integers or 64 bit integers and so on. 62 00:04:38,850 --> 00:04:41,280 So here we could create the integer like this, 63 00:04:41,940 --> 00:04:47,650 well we'll face an issue with that but let's have a look at what got stored. Looks correct, right? 64 00:04:47,880 --> 00:04:49,700 Well let me show you what the issue is 65 00:04:49,710 --> 00:04:56,970 if I add more values here. Let's store the biggest possible number we can store in a long value and that 66 00:04:56,970 --> 00:04:59,660 is 9223, whoops 223, 67 00:04:59,730 --> 00:05:11,550 so 9223372036854775807, 68 00:05:11,550 --> 00:05:15,200 now if I try to store that, I get an error 69 00:05:15,450 --> 00:05:22,980 even though I am in the range of accepted values, so I am in the range of values that are supported. 70 00:05:23,010 --> 00:05:29,520 The problem is that this value which is still a number here is simply too big because it's a double 71 00:05:29,520 --> 00:05:34,320 64 which then well gets wrapped by a number long. 72 00:05:34,320 --> 00:05:36,650 So we should wrap this in quotation marks instead 73 00:05:36,660 --> 00:05:37,930 and now it works 74 00:05:38,070 --> 00:05:39,880 and this is really important to understand, 75 00:05:39,960 --> 00:05:45,390 number int and number long can be used with a number passed as a value and with quotation marks 76 00:05:45,390 --> 00:05:47,080 and you should always use quotation marks, 77 00:05:47,100 --> 00:05:52,290 so basically pass a string because mongodb will then internally kind of convert that string 78 00:05:52,290 --> 00:05:55,010 you could say or simply store it appropriately 79 00:05:55,080 --> 00:06:01,110 but if you pass a number, well the number you pass still faces the javascript limitations here in the 80 00:06:01,110 --> 00:06:03,180 shell which is based on Javascript. 81 00:06:03,240 --> 00:06:08,820 So this number which I try to pass originally simply was too big for the shell, for javascript and 82 00:06:08,820 --> 00:06:14,010 therefore couldn't be stored as a number long because we didn't even reach the mongodb handles it 83 00:06:14,100 --> 00:06:16,170 stage. With a string, 84 00:06:16,170 --> 00:06:18,800 a string is just a list of characters, 85 00:06:18,810 --> 00:06:24,780 it's not treated as a number internally and therefore, it doesn't face the limitations we have with numbers. 86 00:06:24,780 --> 00:06:28,370 So we can then store it like this or pass it to number long like this 87 00:06:28,380 --> 00:06:30,560 and then mongodb can take care about it.