Combine Filtering Documents in MongoDB Collections ($and, $or, $in, $eq, etc)

In this lesson, we learn that combining multiple criteria in MongoDB queries can be done using logical operators such as $and and $or.

We demonstrate how to create queries that filter documents based on various conditions, such as age and skills.

Additionally, we explore the use of the $in operator for matching any values in an array and the $all operator for ensuring all specified values are present.

This lesson provides essential skills for effective use of operators such as: $in, $nin, $lt, $lte, $gt, $gte, $eq, $ne (and many others) in querying MongoDB collections.

Share with a coworker

Transcript

[00:00] We can also combine multiple criteria within the same query using logical operators such as AND, OR, and so on and so forth. So let's take the db.employees.find and here we've got age which is less than 35 and what we can do is to cut this away and to define the AND operator again since this is an operation then it starts with the dollar and here we define what are the criteria that needs to be matched. So one of them could be that age should be less than 35, and the other one could be the one that we've seen before, and that is that among skills, there should be, let's say, JavaScript, but also there should be SQL itself. So in that case, it means that each document which has either JavaScript or SQL within, so at least one of them would be matched, but it also needs to be less than 35H. So we can see that after running this, we have one, two, three results over here.

[01:16] So this is the AND and analogically we have the OR operator which is going to return documents which match either this requirement or that requirement or any other requirements. So we would have at least three documents in the result or more. So what we've seen before is that when we use the in operation within the array then it should be at least any of these skills like JavaScript or SQL in order to match this single requirement. And as we can see, the first one that we can see on the screen, it matches only the SQL. So not all of them, not both JavaScript and SQL, but just one of them.

[02:03] If we wanted to have all of them, then instead of the in operation, we're just running the all JavaScript and SQL. Or in this case, that would be at least that the age is less than 35. So here add a lovelace is included into the result because it has the age 32, here is the age 27, here we've got age 31 etc. It seems that there is no document that would have both of these. So let's replace the JavaScript with Excel and let's redo this query.

[02:42] So that would be Excel. And let's also drop this one just for the sake of the while to see that if all has been applied, then yes, both requirements need to be satisfied. So let's get back again and if we provide the AND over here, then we can actually see that if we have the skills which need the ALL over here, then we could replace it slightly so that if we have the AND operation, then we could say that among skills that should be the Excel skill or there is another possibility that it could have the skills which is SQL itself So both that we could have seen before are equivalent. So this means you need to have Excel and you need to have SQL, which is equivalent to you need to have both Excel and SQL. And as we can see, if we run the new query, then we can see that the only document that has been found has both Excel and SQL at the same time Thank you.