1 00:00:02,130 --> 00:00:06,380 So now before we have a look at other index types mongdb supports, 2 00:00:06,510 --> 00:00:09,850 let's see how we can configure the index types we already know 3 00:00:10,320 --> 00:00:20,430 and for that, let's again have a look at one person in our contacts data and let's create our own unique 4 00:00:20,550 --> 00:00:21,270 index. 5 00:00:22,140 --> 00:00:28,620 The ID index which you get out of the box for this field actually is unique by default 6 00:00:28,650 --> 00:00:30,990 too, this is a setting mongodb gives you 7 00:00:30,990 --> 00:00:38,010 and this ensures that you can't add another document with the same value into the same collection. 8 00:00:38,010 --> 00:00:39,760 Now sometimes you have the use case 9 00:00:39,870 --> 00:00:45,860 that you also need that behavior for a different field and therefore you can add your own unique indexes. 10 00:00:45,900 --> 00:00:51,090 Let's say for e-mail but of course we could have used login uuid if we wanted to but let's use 11 00:00:51,090 --> 00:00:53,570 that top level email element here 12 00:00:53,740 --> 00:00:57,900 and let's create an index on contacts with create index of course, 13 00:00:59,090 --> 00:01:06,120 on email let's say in ascending order and here, I now pass a second argument to create index where I 14 00:01:06,120 --> 00:01:08,670 configure this index, there 15 00:01:08,730 --> 00:01:13,690 I can now set unique to true. If I hit enter, 16 00:01:14,010 --> 00:01:17,930 I get an error that we have some duplicate keys as it seems, 17 00:01:18,090 --> 00:01:19,230 so this generally worked 18 00:01:19,230 --> 00:01:23,250 but it found a duplicate key here for Abigail Clark. 19 00:01:23,250 --> 00:01:32,070 Now let's quickly confirm this by looking into contacts and finding all contacts where the e-mail is 20 00:01:32,100 --> 00:01:33,680 this value here, 21 00:01:33,820 --> 00:01:37,820 Abigail Clark without that backslash. 22 00:01:37,900 --> 00:01:43,410 If I hit enter or I simply count that, I see I have two persons with that indeed 23 00:01:43,810 --> 00:01:46,350 and this is already one advantage of the unique index, 24 00:01:46,450 --> 00:01:52,240 we get such a warning if we want to add it or if we already had it in place and we tried to add a document 25 00:01:52,240 --> 00:01:57,590 with a value that already existed, we would have gotten an error during that insert operation. 26 00:01:57,940 --> 00:02:01,440 So unique indexes can help you as a developer 27 00:02:01,510 --> 00:02:08,560 ensure data consistency and help you avoid duplicate data for fields that you need to have unique, 28 00:02:08,560 --> 00:02:14,430 so the id, _id is unique by default but you have other use cases like maybe that email here 29 00:02:14,430 --> 00:02:14,990 too 30 00:02:15,160 --> 00:02:21,370 and the unique index is a great way for you to not just speed up your find queries but also to guarantee 31 00:02:21,430 --> 00:02:26,540 that you have unique values for that given field in that collection.