Now that was the exists
operator, what is type all about?
Let's go back to our users and find them all
and of course I did not by accident add phone numbers which for two users are strings
and for Max, it's a number.
Now that gives us an opportunity to work with the type operator,
so let's, whoops, on the users collection,
let's find all users where the type of the phone number is a number let's say and for that, I'll look
for that phone number and then I can use type and then specify the type it should be.
Now the types available can be seen in the official docs,
if you check out the type here, you will find the available types and these are the types I also touched
on in my document schema module,
so here you see we got string, we get doubles which are floating point numbers but we also got like the
big integer, decimal 128 which I'll also come back to in the numbers module in this course, the numeric data
module I should say.
So these are two different types of data
you could have and you can check for, also things like object or array or object ID,
so these are the types you can check for.
Now here it's pretty simple,
I want to find all phone numbers, all documents with phone numbers where the phone number is a number. Now number
is an alias that basically sums up floats and integers,
so this does work,
if I would look for a double, it would also work though even though I have no decimal places here by
default since the shell is based on Javascript, a number I insert into my database will be stored as
a floating point number, as a double
because javascript again which drives the shell doesn't know integers and doubles, it only knows doubles
so it takes that and stores it as a double
even if we have no decimal places, this is why I can find this for double too.
You can also use type a bit differently
and you can specify multiple types,
that would be ok,
you then simply pass an array to type
and here you can say it's ok if it's a double or let's say if it's a string and therefore now, I find
all documents because they all either have a double,
so a number or a string,
you can of course also filter for just strings.
So this is the type
operator and how you can use it to make sure you only work with the right type of data when returning
some documents.