Now that we had a brief look at how we can query embedded documents and arrays,
let me come back to my comparison selectors here.
We had a look at most of them but we did not look at $in or $nin,
now what do these two operators do? Now
for that,
let's come back to let's say the runtime. I'll reach out to my movies again and pretty print the results
and the filter I'll specify here
will check the runtime field
and I want to make sure that the runtime is either 30 or 42. Now we know that in our dataset, I could
achieve this with lower than or equal to 42 because we just happen to have data with 30, 42 and 60 minutes
in there
so this will give us all the items that have a runtime lower than or equal 42 which is all the items
with 30 or 42
but of course, that's not the case for every dataset and we might not even know that our data looks like
that.
So if we know there are two discrete values we want to check for,
we can use $in operator. In takes an array and now this does not mean it looks for this array
but in this array, we specify all the values that will be accepted as values for this key,
so in our case 30 and 42.
So it will find us all documents that have a runtime of 30 or 42 but not of 60
or if we had 15 minutes, this would also be excluded.
So if I now hit enter, we get back a bunch of results and you should only find runtime equal to 30 in there
or a runtime equal to 42, like for this document. Now nin is just the opposite,
we could say we want to find everything where the value is not in this range or this is not a range
but in this set of values,
so now we would find all values, all entries where the runtime is not equal to 30 or 42.
So if I now hit enter in our dataset, this will now only include items with a runtime of 60 but again
if you had more values there, like 15 minutes, 20 minutes, these would now also be included in our result
set here.
So that is in and not in, these two missing comparison operators.
And with that we had a look at all comparison operators,
so let's now move on to logical operators in the next module.