Now with logical operators done, let's have a look at element operators and there we got two, exists and
type.
Now what do these do? Now to show what these operators do,
I'll switch to a new database which I'll name user which does not exist yet
and there, I'll have my users collection where
I will insert many users, so some quick insertion training here.
Let's insert some users with a name, Max,
now I'll add some hobbies here which actually is an array where I have embedded documents in the array,
so not just text but there I have hobbies with a title, let's say sports and then also with a frequency,
let's say three times a week, whatever
and then I got another hobby here, let's say title cooking and I do that six times a week,
let's say.
So this is one user, now I'll
also add another field for that user, that's a phone number,
not my real number by the way which might be this number,
again this is not my real number.
Now let's add another user, name
Manuel, hobbies is also an array here with title cooking, frequency five let's say,
not sure if that's the correct one
and then we got another hobby, cars with a frequency of well hard to measure
that, let's say it's looking for new cars twice a week,
I don't know.
So let's say that is this data,
now we also add a phone number
and here it is some text,
so also a number but stored as text, also not his real number.
Now additionally here, we also got an age field of 30. So I added two new documents,
we can have a quick look at them with find pretty,
here's the data from Manuel
and here's my data.
Now I deliberately have different types for phone
and I deliberately added this extra field because that will help us with exists and type.
You might already be able to guess what these operators do, let's say in our huge collection which
we have here with two elements, we want to find all persons who have an age field.
Now we can do that,
users is the collection name and we can pretty print the result by finding all documents where age
and then we pass a document to configure our value for age, where age exists is true, we could also check
for false and we would find all documents where no field exists, with true,
we find all the documents where it does exist.
And if I now hit enter, we should find Manuel who does have an age field.
Now of course, we can also use that together with other arguments,
so we could also say age should exist and should be greater than 30.
If we do that, we find no element because Manuel's fields does exist, he does have an age but he's not greater
than 30, greater than or equal of course would work and would return Manuel again,
so this is exists. Now exist is pretty straightforward,
now let me add one more user to my database here with insert one,
so to my collection, to my users collection. Name is Anna, let's add some hobbies here,
title is sports, frequency, let's say it's two
and here we have another title yoga, frequency is three, let's say. Now for Anna,
I will also add a phone number, let's say it's also text and also not the real one
and then I also add an age here but I'll set it to null, so it's not this closed
you could say. Now technically that makes a difference because if I now find all elements real quick,
we see that for Anna, there is an age field, the value just is null which is a special value which is,
well it just means there is no value there but the field exists.
Now if I repeat my query from before where I just check for the age field existing, I do find Anna too because
the field exists, it just happens to be null.
Now sometimes you want to find all users where it exists and where there is a valid value in there though,
so now we could check false,
maybe it's included in there but no, with exists false, you only find Max who doesn't have an age field.
So if you want to make sure that age exists but also, that there is a value in there, you have to check
that it also is not equal to null,
so now I check for all documents where the age field exists and that should of course be true and not
age,
so where the age field exists and where the age field is not equal to null
and now this will return Manuel again.
So this is how you can use exists and especially this combination with not equal null is quite
helpful for making sure you really only have documents where there is a value in that field.