1 00:00:02,340 --> 00:00:09,690 So which important number types do we have to differentiate? And these are mostly integers, longs or long 2 00:00:09,690 --> 00:00:13,690 integers and doubles of which we also have different types, 3 00:00:13,980 --> 00:00:20,880 to be precise we can work with these four types of numbers in mongodb. We can have integers, so-called 4 00:00:21,010 --> 00:00:24,750 int 32 integers in mongodb to be precise, 5 00:00:24,870 --> 00:00:26,580 these are full numbers, 6 00:00:26,610 --> 00:00:29,740 so we have no decimal places here. For long, 7 00:00:29,760 --> 00:00:36,540 it's the same, these are also full numbers and I'll talk about the difference between int 64 and int 32 in 8 00:00:36,540 --> 00:00:37,180 a second, 9 00:00:37,320 --> 00:00:44,520 so we get two types of data which are only about storing full numbers. And then we got doubles, also 64 10 00:00:44,520 --> 00:00:48,510 bit which do actually store numbers with decimal places, 11 00:00:48,510 --> 00:00:56,400 so 1.57 could be stored in a double and actually that double, that 64 bit double is the default value 12 00:00:56,400 --> 00:01:01,680 type mongodb uses if you pass a number with no extra information, no matter, 13 00:01:01,680 --> 00:01:07,800 by the way that is important, no matter if that number is theoretically an integer and has no decimal 14 00:01:07,800 --> 00:01:08,940 place or not, 15 00:01:08,970 --> 00:01:15,160 it will be stored as a 64 bit double when passing in the number through the shell. 16 00:01:15,330 --> 00:01:20,680 We also have a special type, high precision doubles in mongodb, 17 00:01:20,910 --> 00:01:23,330 these are also numbers with decimal places 18 00:01:23,400 --> 00:01:26,100 but there's one important difference to the normal doubles, 19 00:01:26,190 --> 00:01:32,130 so let's compare all these types then to find out what the differences are. Int 32 integers 20 00:01:32,190 --> 00:01:39,210 are full numbers in this range, quite a big range and a range that will suffice for a lot of things 21 00:01:39,210 --> 00:01:46,500 you might be doing in your application, since you can store numbers up to 2.1 billion roughly and the 22 00:01:46,500 --> 00:01:47,880 same in the negative range, 23 00:01:48,030 --> 00:01:52,730 you should have plenty of space for a lot of the things you do in a lot of applications. 24 00:01:52,830 --> 00:01:55,300 But sometimes this range is not big enough, 25 00:01:55,320 --> 00:01:58,440 sometimes you simply work with bigger numbers, 26 00:01:58,440 --> 00:02:01,110 for that you can use longs, long integers. 27 00:02:01,110 --> 00:02:08,820 Here you can store way bigger numbers which then definitely should suffice for the types of calculations 28 00:02:08,850 --> 00:02:12,210 or work you might be doing in your application. 29 00:02:12,210 --> 00:02:18,420 If you would store even bigger numbers, you would have to kind of split it up into two fields which you 30 00:02:18,420 --> 00:02:20,400 then manually have to combine in well 31 00:02:20,400 --> 00:02:23,590 however you are processing these numbers. Now 32 00:02:23,610 --> 00:02:24,790 these are the integers 33 00:02:24,840 --> 00:02:28,440 so we're only talking about full numbers with no decimal places here 34 00:02:28,830 --> 00:02:35,220 but of course in a lot of applications, let's say product prices, you have decimal places, your product 35 00:02:35,220 --> 00:02:39,090 might cost 9.99 and not $10, 36 00:02:39,180 --> 00:02:43,010 so for that you got doubles and there, that is important, 37 00:02:43,050 --> 00:02:45,490 the decimal values are approximated 38 00:02:45,600 --> 00:02:47,970 and I will show you what this means in a second. 39 00:02:48,090 --> 00:02:53,550 But it's really important here to understand that for these normal 64 bit doubles, the decimal places 40 00:02:53,640 --> 00:02:59,060 are approximated, not guaranteed or not stored with high precision 41 00:02:59,190 --> 00:03:00,930 and that's exactly the difference to the 42 00:03:01,050 --> 00:03:08,370 well, high precision doubles or decimals as they are also called. There, the decimal values are stored 43 00:03:08,370 --> 00:03:09,350 with high precision 44 00:03:09,390 --> 00:03:13,590 up to 34 decimal digits are stored with high precision, 45 00:03:13,620 --> 00:03:21,290 so you are guaranteed that if you enter 9.9999999, it will be stored as such and not stored differently. 46 00:03:21,300 --> 00:03:26,410 Now you might be wondering why would a double store it with less precision. 47 00:03:26,640 --> 00:03:31,530 Well that's not some bad will by the people who develop mongodb or anything like that, 48 00:03:31,560 --> 00:03:39,210 these are limitations you face in computing due to the way numbers are stored or how computing works 49 00:03:39,210 --> 00:03:41,540 in general with bits and so on. 50 00:03:41,640 --> 00:03:46,830 Now diving deeper into this would be way beyond the scope of this course as it is a highly theoretical 51 00:03:46,830 --> 00:03:48,470 and mathematical topic 52 00:03:48,660 --> 00:03:53,490 but in the last lecture of this module, you will find some links to discussions that might be interesting 53 00:03:53,490 --> 00:03:57,990 to you if you want to dive into all the theory here and want to learn way more. 54 00:03:57,990 --> 00:03:59,630 For now we just take it as granted, 55 00:03:59,730 --> 00:04:06,120 doubles for some technical reasons are not guaranteed, so the decimal places there are not guaranteed to be 56 00:04:06,120 --> 00:04:06,540 correct, 57 00:04:06,540 --> 00:04:11,760 they are approximated, for the decimals so the 128 bit doubles, 58 00:04:11,760 --> 00:04:15,780 you have a guarantee for up to 34 decimal digits. 59 00:04:15,840 --> 00:04:23,800 The way this works is that essentially these 128 bit decimals have a lower range, 60 00:04:23,820 --> 00:04:32,290 so the part before the comma is lower therefore you have more precision after the comma, for doubles, 61 00:04:32,310 --> 00:04:33,860 you have a higher range, 62 00:04:33,930 --> 00:04:41,930 so the integer part is bigger but the increased size there is basically one by losing precision 63 00:04:41,940 --> 00:04:47,820 after the comma you could say. So this is the rough overview and we'll see all that in the shell 64 00:04:47,880 --> 00:04:54,510 and in practice in a second. As a first quick summary, the idea of course is that you use integers 65 00:04:54,510 --> 00:05:01,520 here for normal integers, if you know you store some value which is a number in that range here 66 00:05:01,790 --> 00:05:06,920 and which only is an integer, consider using an integer and you should consider using that because it will 67 00:05:06,920 --> 00:05:12,410 simply take up less space than if you just enter it as a normal value and therefore it automatically 68 00:05:12,410 --> 00:05:20,660 gets stored as a 64 bit double. Use a long if you need it, if your range for int 32 integers is not enough, 69 00:05:20,690 --> 00:05:23,780 use it a long if you only work with full numbers. 70 00:05:23,780 --> 00:05:28,980 You can use doubles for basically all values where you don't need high precision you could say, 71 00:05:29,090 --> 00:05:32,090 so this is your quick and lazy approach to storing numbers 72 00:05:32,150 --> 00:05:37,970 but it's also a valid approach for storing numbers that have digits, that have decimal places where you 73 00:05:37,970 --> 00:05:40,160 just don't need high precision. 74 00:05:40,640 --> 00:05:44,040 And we of course have the high precision doubles here 75 00:05:44,120 --> 00:05:50,720 if you need that high precision for calculations with monetary data, let's say or for scientific calculations. 76 00:05:51,320 --> 00:05:53,330 You will have noted that throughout the course, 77 00:05:53,330 --> 00:06:00,230 we always just used doubles, so just entered a value and therefore, we automatically used the 64 bit doubles 78 00:06:00,230 --> 00:06:00,720 here 79 00:06:00,920 --> 00:06:06,890 and that was simply also to not focus on the numbers too much there and simply get the values into the 80 00:06:06,890 --> 00:06:07,490 database, 81 00:06:07,520 --> 00:06:12,600 focus on the different parts we explained like the operators, how to query stuff and so on. 82 00:06:12,800 --> 00:06:17,810 If you're building an application and you know you're storing something like an age, you should at least 83 00:06:17,810 --> 00:06:24,290 consider taking an int 32 instead of the lazy route of using a double because it will be smaller but more 84 00:06:24,290 --> 00:06:25,840 on that in the next lectures 85 00:06:25,860 --> 00:06:26,100 now.