Filtering Documents using Expressions in MongoDB Collections ($expr, $size, $in, $and, etc)

In this lesson, we learn that we can perform advanced filtering in MongoDB by using expressions within our queries. All thanks to $expr.

We demonstrate how to compare multiple document properties, such as ensuring age is less than salary using comparison operators ($lt, $lte, $gt, $in, $nin, etc).

Additionally, we explore filtering based on the length of an array ($size) to check for specific skills.

Share with a coworker

Transcript

[00:00] We can perform even more advanced filtering thanks to the expression which we can pass into the query predicate. So we're going to use find again, but this time instead of relating to an existing property directly existing on the document, we're going to pass an expression that could relate to multiple different properties of the documents or perform even something more advanced. Still, since this is a part of the Mongo engine, we need to prepend it with the dollar character. So the expression itself could be pretty much anything that we want. So let's say that we're going to compare the age property and the salary property.

[00:43] Obviously, age is going to be smaller than salary. Still, the expression right now knows what we want to compare, but we didn't specify how do we want to compare it. So we need to wrap it again with what is the type of the comparison. So the comparison operator in this case could be less than, age is less than salary. So due to the fact that these properties are being used within the expression part, we need them to be prepended with dollars as well.

[01:16] This is just a syntax requirement. So let's run it and let's see that there is quite a lot of these documents in the result since obviously all age values are going to be smaller than salary which is what we definitely wanted. Now expression is just a regular part of the query predicate thing just the same way as others so we can put it in something bigger. So let's go back to the end part and we're going to provide two criteria that need to be satisfied. Now we're going to paste the expression thing but note that we are in the array literal of the end and expression needs to be its own literal.

[02:02] So I'm going to cut it again and just paste it into the object literal. Now everything is syntactically correct. And let's say that we want only those employees who have the SQL skill and the comma over here. Now, if we run it, then we can see that there would be one, two, three documents that have been returned. And let's provide the final example where we are going to carry on with the skills.

[02:31] So let's remove this part over here. So we want the employees who will include the skills SQL within their skill array. And our expression is going to figure out how many skills does the employee document have right now. So what we're going to compare is that whether the length of skills is going to be bigger than 4. So let me just cut this array away because we need to start with what is the comparison operator.

[03:08] So let's say that this is going to be greater than or equal. Now we want to compare the skills length with the number. So if we wrap this one, the skills, with an object literal and we're going to check what is the size of the array that is under the property of skills. Now we need to make this a string literal obviously and since we are inside the expression part we also need to prepend this one with the dollar so here we can see that this is just one requirement this is another requirement but since this is going to be calculated dynamically We need to make it an expression. So we are comparing two things whether the left-hand side is greater than or equal And this is the left-hand side which could have been just the skills value But since we want to pre-process it first, then we need to wrap it into an object literal with yet another operator and this is the right hand side, so this is 4 so as we can see there is quite a bit of these square and curly braces but still this does the job so there is SQL, there is at least 4, there is SQL, there is at least four, there is SQL, there is at least four, again there is SQL and there is at least four, everything is correct.