Get startedGet started for free

Query Operators

1. Query Operators

Hi again! In this lesson, you’ll learn how to use query operators to build more powerful filters so you can find exactly the documents you’re looking for.

2. The $in operator

A first useful operator is $in. You use $in when you want to find documents where a field’s value matches any value in a list. You can use it to build a filter like this: the top-level key is release_year — but this time, the value is another dictionary with "$in" as key and then a list of release years. It almost reads like a sentence: "Find all documents where the release year is in 2008 or 2009." It's a simple way to match one field against multiple possible values.

3. The $in operator on arrays

You may remember from the previous chapter that query filters also work on fields that hold arrays, like this example, which finds the first movie with "adventure" in its list of genres. Well, the $in operator works the same way with arrays. This query will return the first movie where the genre field includes either "action" or "thriller". And notice that I used find_one() here instead of find() this time. That's just to get a single result quickly. You can use query operators like $in with both methods.

4. The $exists operator

Another operator is $exists, which you use to check if a field exists. This is especially useful because MongoDB has a flexible schema, so not every field in a document may be set. This example here reads as: “Give me all documents where the won_oscar field is present—regardless of its value.” So even movies where won_oscar is False will be returned— as long as the field is there. When we convert the cursor to a list, we get exactly that: only documents where won_oscar is set.

5. Combining queries with $and

Sometimes, you’ll want to combine multiple conditions in a query. You can do that using the $and and $or operators. Let’s look at an example using $and. Yes—it’s a bit heavy on square and curly brackets, but when you read it out loud, it’s clear: "Find the first movie that was released in 2023 and has the genre 'comedy'." Actually, you can write this in a much simpler way too. MongoDB applies an implicit $and when you pass multiple key-value pairs in the filter. So this shorter version does the exact same thing.

6. Combining queries with $or

With $and, all conditions must be met. But with $or, it’s enough if just one of the conditions is true. Let’s take the same query as before, but swap $and with $or, and find all movies that match instead of just the first match. Now, instead of finding movies that were released in 2023 and are comedies, we’ll get movies that were either released in 2023 or have the genre 'comedy', or both, as you can tell from the output. Superbad is a comedy, so it's included even though its release year is not 2023, and megan was released in 2023, so it's included as well, even though it's not a comedy movie.

7. Let's practice!

With these new operators under your belt, you're ready to jump into the exercises and put them into practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.