1 00:00:01,900 --> 00:00:09,670 Now with logical operators done, let's have a look at element operators and there we got two, exists and 2 00:00:09,670 --> 00:00:10,540 type. 3 00:00:10,540 --> 00:00:15,530 Now what do these do? Now to show what these operators do, 4 00:00:15,530 --> 00:00:19,740 I'll switch to a new database which I'll name user which does not exist yet 5 00:00:20,010 --> 00:00:24,600 and there, I'll have my users collection where 6 00:00:24,840 --> 00:00:29,550 I will insert many users, so some quick insertion training here. 7 00:00:29,550 --> 00:00:33,070 Let's insert some users with a name, Max, 8 00:00:33,120 --> 00:00:39,590 now I'll add some hobbies here which actually is an array where I have embedded documents in the array, 9 00:00:39,600 --> 00:00:47,230 so not just text but there I have hobbies with a title, let's say sports and then also with a frequency, 10 00:00:47,460 --> 00:00:50,490 let's say three times a week, whatever 11 00:00:50,880 --> 00:00:58,700 and then I got another hobby here, let's say title cooking and I do that six times a week, 12 00:00:58,750 --> 00:01:00,740 let's say. 13 00:01:00,920 --> 00:01:03,090 So this is one user, now I'll 14 00:01:03,200 --> 00:01:08,100 also add another field for that user, that's a phone number, 15 00:01:08,120 --> 00:01:11,230 not my real number by the way which might be this number, 16 00:01:11,290 --> 00:01:13,610 again this is not my real number. 17 00:01:13,610 --> 00:01:17,750 Now let's add another user, name 18 00:01:17,750 --> 00:01:31,070 Manuel, hobbies is also an array here with title cooking, frequency five let's say, 19 00:01:31,160 --> 00:01:33,330 not sure if that's the correct one 20 00:01:33,570 --> 00:01:40,310 and then we got another hobby, cars with a frequency of well hard to measure 21 00:01:40,310 --> 00:01:43,490 that, let's say it's looking for new cars twice a week, 22 00:01:43,620 --> 00:01:44,400 I don't know. 23 00:01:44,490 --> 00:01:46,470 So let's say that is this data, 24 00:01:46,480 --> 00:01:47,790 now we also add a phone number 25 00:01:47,820 --> 00:01:49,510 and here it is some text, 26 00:01:49,680 --> 00:01:54,630 so also a number but stored as text, also not his real number. 27 00:01:54,630 --> 00:02:00,660 Now additionally here, we also got an age field of 30. So I added two new documents, 28 00:02:00,810 --> 00:02:03,430 we can have a quick look at them with find pretty, 29 00:02:03,560 --> 00:02:05,070 here's the data from Manuel 30 00:02:05,070 --> 00:02:06,510 and here's my data. 31 00:02:06,510 --> 00:02:09,340 Now I deliberately have different types for phone 32 00:02:09,480 --> 00:02:14,580 and I deliberately added this extra field because that will help us with exists and type. 33 00:02:14,970 --> 00:02:20,940 You might already be able to guess what these operators do, let's say in our huge collection which 34 00:02:20,940 --> 00:02:29,190 we have here with two elements, we want to find all persons who have an age field. 35 00:02:29,240 --> 00:02:30,510 Now we can do that, 36 00:02:30,590 --> 00:02:39,370 users is the collection name and we can pretty print the result by finding all documents where age 37 00:02:39,500 --> 00:02:46,550 and then we pass a document to configure our value for age, where age exists is true, we could also check 38 00:02:46,550 --> 00:02:51,290 for false and we would find all documents where no field exists, with true, 39 00:02:51,380 --> 00:02:54,910 we find all the documents where it does exist. 40 00:02:55,250 --> 00:03:00,900 And if I now hit enter, we should find Manuel who does have an age field. 41 00:03:00,950 --> 00:03:03,820 Now of course, we can also use that together with other arguments, 42 00:03:03,890 --> 00:03:09,520 so we could also say age should exist and should be greater than 30. 43 00:03:09,650 --> 00:03:16,160 If we do that, we find no element because Manuel's fields does exist, he does have an age but he's not greater 44 00:03:16,160 --> 00:03:21,390 than 30, greater than or equal of course would work and would return Manuel again, 45 00:03:21,590 --> 00:03:25,900 so this is exists. Now exist is pretty straightforward, 46 00:03:25,910 --> 00:03:30,400 now let me add one more user to my database here with insert one, 47 00:03:30,540 --> 00:03:37,410 so to my collection, to my users collection. Name is Anna, let's add some hobbies here, 48 00:03:38,410 --> 00:03:45,280 title is sports, frequency, let's say it's two 49 00:03:45,440 --> 00:03:54,590 and here we have another title yoga, frequency is three, let's say. Now for Anna, 50 00:03:54,700 --> 00:04:00,300 I will also add a phone number, let's say it's also text and also not the real one 51 00:04:00,340 --> 00:04:06,110 and then I also add an age here but I'll set it to null, so it's not this closed 52 00:04:06,110 --> 00:04:12,910 you could say. Now technically that makes a difference because if I now find all elements real quick, 53 00:04:13,290 --> 00:04:18,910 we see that for Anna, there is an age field, the value just is null which is a special value which is, 54 00:04:19,090 --> 00:04:23,310 well it just means there is no value there but the field exists. 55 00:04:23,360 --> 00:04:33,190 Now if I repeat my query from before where I just check for the age field existing, I do find Anna too because 56 00:04:33,220 --> 00:04:36,660 the field exists, it just happens to be null. 57 00:04:36,890 --> 00:04:43,180 Now sometimes you want to find all users where it exists and where there is a valid value in there though, 58 00:04:43,190 --> 00:04:45,190 so now we could check false, 59 00:04:45,200 --> 00:04:51,770 maybe it's included in there but no, with exists false, you only find Max who doesn't have an age field. 60 00:04:51,830 --> 00:04:58,820 So if you want to make sure that age exists but also, that there is a value in there, you have to check 61 00:04:59,030 --> 00:05:01,950 that it also is not equal to null, 62 00:05:01,950 --> 00:05:09,150 so now I check for all documents where the age field exists and that should of course be true and not 63 00:05:09,170 --> 00:05:09,660 age, 64 00:05:09,830 --> 00:05:14,420 so where the age field exists and where the age field is not equal to null 65 00:05:14,510 --> 00:05:16,890 and now this will return Manuel again. 66 00:05:17,000 --> 00:05:22,130 So this is how you can use exists and especially this combination with not equal null is quite 67 00:05:22,130 --> 00:05:27,820 helpful for making sure you really only have documents where there is a value in that field.