1 00:00:01,930 --> 00:00:02,380 Ok 2 00:00:02,400 --> 00:00:05,640 so let's now play around with these data types a bit, 3 00:00:05,640 --> 00:00:10,370 I'm on my MongoDB server again and I dropped all existing databases, 4 00:00:10,810 --> 00:00:16,440 you can by the way always do that by switching to the database like test if I had that database here and 5 00:00:16,440 --> 00:00:17,520 then executing 6 00:00:17,520 --> 00:00:22,270 drop database, db.dropdatabase and this will get rid of the database 7 00:00:22,290 --> 00:00:25,010 if you have it. But let me now clear the screen 8 00:00:25,030 --> 00:00:28,050 and let's now use a new database, 9 00:00:29,790 --> 00:00:33,050 companies or company data maybe 10 00:00:33,310 --> 00:00:39,480 and in there, let's say I have a company's collection and in that company's collection, I now want to insert one new 11 00:00:39,480 --> 00:00:46,680 company and my idea here is that for this company, I want to showcase some of these data types. So that 12 00:00:46,680 --> 00:00:50,420 company will probably have a name and that name will be some text. 13 00:00:50,510 --> 00:00:55,500 Now as you learned, the text is enclosed in quotation marks and you can by the way use double or single 14 00:00:55,500 --> 00:00:56,640 quotation marks here, 15 00:00:56,640 --> 00:00:57,960 this does not matter, 16 00:00:58,020 --> 00:01:03,390 that is the case in the shell here because the shell is based on javascript, for the programming language 17 00:01:03,390 --> 00:01:06,920 you're using with the MongoDB driver, that might differ of course. 18 00:01:07,020 --> 00:01:11,390 So here, let's give this any name, like Fresh Apples Inc, it's 19 00:01:11,700 --> 00:01:17,260 a company I just came up with and now with that company name added here, 20 00:01:17,790 --> 00:01:19,140 what else could we add? 21 00:01:19,440 --> 00:01:26,880 Well let's say this company is a startup and therefore we add a isStartup key here and this simply is well 22 00:01:26,910 --> 00:01:27,460 true, 23 00:01:27,510 --> 00:01:33,180 so here I'm using a boolean because the case or the question whether this is a startup or not can be 24 00:01:33,180 --> 00:01:34,830 answered with yes or no 25 00:01:34,980 --> 00:01:37,940 and translated to a boolean, that would be true or false, 26 00:01:37,980 --> 00:01:38,730 here it is yes, 27 00:01:38,760 --> 00:01:39,910 so it's true. 28 00:01:40,440 --> 00:01:47,490 Now besides that being a startup, let's also log its number of employees and I'll add 33 here. 29 00:01:47,640 --> 00:01:54,270 So this is a number and this is actually an integer you would think at least, now the shell which 30 00:01:54,270 --> 00:02:00,420 I'm using here is based on javascript and javascript doesn't differentiate between integers and floating 31 00:02:00,420 --> 00:02:01,420 point numbers, 32 00:02:01,440 --> 00:02:04,110 this would be a floating point number by the way. 33 00:02:04,170 --> 00:02:11,540 So this will actually be stored as a double or float in MongoDB, not as this number decimal special 34 00:02:11,550 --> 00:02:16,950 high precision thing I mentioned on the slide but a normal floating point number with some imprecision 35 00:02:17,190 --> 00:02:19,520 on the decimal places. 36 00:02:19,590 --> 00:02:21,140 Now that might be fine 37 00:02:21,330 --> 00:02:24,820 and for now, let's simply store it like that, 38 00:02:25,800 --> 00:02:33,660 let's also add a funding here and let's store a very large number there and I'll actually store a number 39 00:02:33,660 --> 00:02:37,670 which is easy to remember by just sliding over my numbers twice, 40 00:02:37,740 --> 00:02:47,480 so it's 1234567890 and then again, let's also add an embedded document here, details 41 00:02:47,610 --> 00:02:55,730 maybe and there we could simply have any additional data we want like CEO Mark Super, 42 00:02:55,730 --> 00:02:58,110 obviously a good name for a CEO 43 00:02:58,130 --> 00:03:07,220 and let's also add a list, tax maybe. That is an array, you always add an array by using square brackets 44 00:03:07,490 --> 00:03:10,940 and there in turn, we can add any value we want, 45 00:03:11,180 --> 00:03:17,660 so this can hold multiple string values like super perfect, whatever you want, 46 00:03:17,660 --> 00:03:23,900 it could also hold numbers and all the values in the array are just separated by commas 47 00:03:24,140 --> 00:03:31,400 or it could of course also hold other nested documents, so this could hold other documents with tax which 48 00:03:31,460 --> 00:03:35,560 are well basically documents with a title that then holds the value, 49 00:03:35,570 --> 00:03:40,060 so whatever your application needs is possible, you're really flexible here. 50 00:03:40,190 --> 00:03:45,210 So here I go for the embedded document in an array style. 51 00:03:45,380 --> 00:03:50,830 Now besides these values, we also learned about dates and timestamps, 52 00:03:50,900 --> 00:03:59,340 so if we also add the foundingdate here, we can construct a new date in the shell by calling new date 53 00:03:59,510 --> 00:04:05,140 and if we add it like this, it will take the current date and I will indeed do that here. 54 00:04:05,300 --> 00:04:13,240 We can also add a timestamp like insertedAt to give that a clear timestamp when this data was 55 00:04:13,270 --> 00:04:17,080 inserted by calling new timestamp. 56 00:04:17,080 --> 00:04:21,860 Now these are functions, new date and new timestamp that are provided by the shell, 57 00:04:22,090 --> 00:04:27,160 there are equivalents in the drivers for the different programming languages though and you'll find 58 00:04:27,160 --> 00:04:31,080 the exact methods you need to call in the driver docs. 59 00:04:31,090 --> 00:04:35,100 Here for example, I'm on the page of the MongoDB Nodejs driver 60 00:04:35,260 --> 00:04:38,210 and there I can click on the reference of the latest release 61 00:04:38,350 --> 00:04:43,840 and of course such release references are available for all drivers and in the documentation for the 62 00:04:43,840 --> 00:04:44,340 driver, 63 00:04:44,350 --> 00:04:49,600 you find detailed instructions on installation and how to use it and you should also find an API 64 00:04:49,660 --> 00:04:55,530 documentation which is simply a list of all the features that are included and in there for example 65 00:04:55,540 --> 00:05:01,690 you also find long and int32 that will be useful methods or objects when working with these values 66 00:05:02,500 --> 00:05:06,370 and you also will find something that helps you with a timestep for example. 67 00:05:06,430 --> 00:05:11,940 So down there, if you click on timestamp, you open the timestampe doc and you learned how you can create 68 00:05:11,940 --> 00:05:14,470 a timestamp in that NodeJS driver, 69 00:05:14,470 --> 00:05:22,480 so there by calling new timestamp after importing the timestamp from this mongodb driver. 70 00:05:22,750 --> 00:05:26,800 And more information on that and how to use that driver and so on can 71 00:05:26,830 --> 00:05:33,240 as I mentioned before we found in the general driver docs, like the tutorials here, the quickStart or the 72 00:05:33,250 --> 00:05:39,820 general reference here, so that is how you can use these times you learned about here in a shell in 73 00:05:39,820 --> 00:05:41,060 your driver too. 74 00:05:41,290 --> 00:05:46,360 So the shell here is based on Javascript, therefore here we actually do use new timestamp to create a 75 00:05:46,360 --> 00:05:47,210 timestamp 76 00:05:47,530 --> 00:05:50,220 and with that for now I added all the data I want to add. 77 00:05:50,410 --> 00:05:52,960 If I now hit enter, this gets inserted 78 00:05:53,110 --> 00:05:56,930 and now let's have a look at it by finding one company, 79 00:05:56,950 --> 00:06:01,090 so the one we just added. And here we see that company was added, 80 00:06:01,270 --> 00:06:03,480 we see the data we added, 81 00:06:03,490 --> 00:06:09,310 we see for example for the date, we got this ISO date object with the current date 82 00:06:09,400 --> 00:06:12,500 I'm recording this at as a value entered here. 83 00:06:12,670 --> 00:06:19,330 You see it uses the year year year, month month, day day format then a T to separate the time from the 84 00:06:19,330 --> 00:06:25,710 date and then we got the time in a 24 hour format stored here, 85 00:06:26,030 --> 00:06:30,290 the set at the end marks. the timezone for which this time is logged. 86 00:06:30,310 --> 00:06:32,580 You also see that timestep object 87 00:06:32,590 --> 00:06:38,750 and as I mentioned this is actually a timestamp based on the current time in milliseconds 88 00:06:38,890 --> 00:06:46,190 and also it has an ordinal component so that if we were to insert two elements simultaneously with 89 00:06:46,210 --> 00:06:49,880 insertMany, they would have different timestamps. 90 00:06:49,930 --> 00:06:54,610 Now one super interesting thing can be seen if we have a look at funding, 91 00:06:54,700 --> 00:06:59,930 there you see that the number that is stored is actually not the number that I added, 92 00:07:00,040 --> 00:07:07,180 if I scroll up a bit, you see that for funding I did add 1234567890 twice essentially, 93 00:07:07,210 --> 00:07:15,460 so I rolled over the keyboard twice but the number that was stored is 123456789012 94 00:07:15,460 --> 00:07:20,410 34567 and then it's essentially nullified or cut off 95 00:07:20,410 --> 00:07:27,370 you could say. The reason for that is that I tried to store a number that was too big because the normal 96 00:07:27,370 --> 00:07:33,670 number javascript accepts is essentially a 64bit floating point value 97 00:07:33,840 --> 00:07:35,550 and that could not be stored here 98 00:07:35,680 --> 00:07:38,650 and that is a restriction you can face when working with numbers. 99 00:07:38,770 --> 00:07:44,950 If you need to store super big numbers, you'll have to store them separately, maybe as a string. 100 00:07:44,950 --> 00:07:49,520 Now speaking of numbers, you saw some general values here on the company, 101 00:07:49,660 --> 00:07:55,330 now let me get a little bit more abstract so that I can show you what's up with these numbers and I 102 00:07:55,330 --> 00:08:00,490 have a whole module where I also talk about numbers and how to work with number data 103 00:08:00,580 --> 00:08:06,910 but for now, let me quickly add some dummy data. I'll use a new collection for this, numbers and this will 104 00:08:06,910 --> 00:08:14,860 be one of the rare cases where I'll just use an abstract example. I'll insert one document here, a 1, 105 00:08:14,950 --> 00:08:22,910 now if I insert it like this, obviously if I access numbers and I find one element, we see it here. 106 00:08:22,930 --> 00:08:29,290 Now one interesting thing can be seen if I now use db.stats, stats is a utility method provided by 107 00:08:29,320 --> 00:08:33,950 the shell which outputs some well, stats about this database. 108 00:08:34,060 --> 00:08:37,670 Here we see like how many collections we got in there, how many objects 109 00:08:37,870 --> 00:08:43,850 and we also see the average object size. 110 00:08:43,900 --> 00:08:46,080 Now I got two collections in there, 111 00:08:46,210 --> 00:08:54,040 so let me quickly get rid of that company's collection by calling drop on the collection, like drop database 112 00:08:54,040 --> 00:08:57,990 gets rid of a database, drop on a collection gets rid of that database. 113 00:08:58,030 --> 00:09:03,670 If I now repeat stats, we see there's only one collection there and we get a data size, an average data size 114 00:09:04,120 --> 00:09:05,920 of 33, 115 00:09:05,950 --> 00:09:16,530 now remember that value of 33 here. Now I will use my numbers collection and delete all elements, 116 00:09:16,530 --> 00:09:19,950 so the one element I got in there, if I now execute db.stats, 117 00:09:19,950 --> 00:09:23,900 we obviously see data size zero because there's nothing in there. 118 00:09:23,910 --> 00:09:25,250 Now let me repeat that 119 00:09:25,290 --> 00:09:32,130 insert one command here but I'll not insert a number like this, which remember is stored as a 64bit 120 00:09:32,130 --> 00:09:37,980 floating point number by default but I'll use a special method provided by the shell and you'll find 121 00:09:37,980 --> 00:09:44,250 equivalence in your driver docs when working with a driver which will create an int32 value instead 122 00:09:44,250 --> 00:09:45,960 of that default 64 bit 123 00:09:45,960 --> 00:09:47,440 floating point number 124 00:09:47,730 --> 00:09:56,750 and if I store the same number one, now with this helper function and I repeat my stats output, you'll 125 00:09:56,750 --> 00:09:58,740 see that it shrank a bit 126 00:09:59,090 --> 00:10:04,790 and this is one of the reasons why you could consider using these special values provided by mongodb 127 00:10:04,790 --> 00:10:09,730 because you're able to manage your data size a bit more efficiently. 128 00:10:09,740 --> 00:10:16,160 Now it's also worth pointing out that this was only stored as this 64bit floating point number because 129 00:10:16,160 --> 00:10:22,180 the shell is based on javascript and javascript doesn't differentiate between integers and floats, 130 00:10:22,180 --> 00:10:23,540 other languages do 131 00:10:23,600 --> 00:10:24,850 so for other languages, 132 00:10:24,950 --> 00:10:30,680 this might not be the case if you accessed your database through the driver and added a number there, for 133 00:10:30,710 --> 00:10:31,460 the shell 134 00:10:31,460 --> 00:10:36,200 it is however and this is good because here I can show you the difference between that default number, 135 00:10:36,470 --> 00:10:39,110 the float and this integer. 136 00:10:39,320 --> 00:10:46,280 You can also check the type of a value as it is stored in mongodb by using the type of command which 137 00:10:46,280 --> 00:10:48,280 is provided in the shell. There 138 00:10:48,410 --> 00:10:50,240 I can access numbers, 139 00:10:50,240 --> 00:10:56,580 find one to get that one element I have and then access the a key with .a and we see the type here is 140 00:10:56,600 --> 00:10:57,980 number. 141 00:10:58,100 --> 00:11:01,640 Now I don't want to go too in-depth into the whole number thing, 142 00:11:01,670 --> 00:11:07,700 also because it has a lot to do with computing in general and not so much with mongodb but I've found 143 00:11:07,730 --> 00:11:11,770 this important to highlight that you have these different formats. 144 00:11:11,860 --> 00:11:16,650 Often it might not matter and the default which is stored might work for you 145 00:11:16,690 --> 00:11:22,330 like for all the numbers we used thus far for ages or prices, we can just enter the number and it will get 146 00:11:22,330 --> 00:11:23,500 stored in the database, 147 00:11:23,500 --> 00:11:26,080 the sizing difference might not really matter for us, 148 00:11:26,230 --> 00:11:31,960 the extra precision we could get on the decimal places will also not matter for only two or three decimal 149 00:11:31,960 --> 00:11:32,750 places, 150 00:11:32,890 --> 00:11:35,520 so there we will be fine with the defaults, 151 00:11:35,590 --> 00:11:41,860 I want you to be aware of the general set of data types you have and that there are specialized data 152 00:11:41,860 --> 00:11:44,550 types for specialized kind of operations 153 00:11:44,650 --> 00:11:45,950 you might plan. 154 00:11:46,360 --> 00:11:52,240 You should also not miss the bson types document to which you find a link in the last lecture of this module 155 00:11:52,660 --> 00:11:55,060 where you can learn more about the general data types 156 00:11:55,060 --> 00:12:01,540 mongodb knows and how it can store data in its database and where you can then also learn more 157 00:12:01,660 --> 00:12:08,020 about some of the special data types like objectid or timestamps and which in general is a great backup 158 00:12:08,020 --> 00:12:13,420 if you want to dive deeper into a specific data type or learn more about a specific data type.