Get startedGet started for free

Finding documents

1. Finding documents

In the last lesson, we learned about databases, collections, and documents. We also learned how data structures in MongoDB relate to those of JSON and Python. In this lesson, we will learn how to query a collection to find documents of interest.

2. An example "laureates" document

Here we have an example document in the laureates collection. Finding documents in MongoDB reminds me of the work of this particular laureate. The remarkable rays named after him are today called x-rays.

3. Filters as (sub)documents

To find documents satisfying some criteria, we express those criteria as a document. This filter document mirrors the structure of documents to match in the collection. Imagine a filter document as an x-ray image like the one on the right. This image is one of the first x-ray images ever recorded. Imagine you use it to filter by eye a collection of color photos of people's hands. You might keep ones with five fingers and a ring on the ring finger.

4. The Walrus is Out

Here's another way to think about filter documents. Hold a pair of pants up to a collection of mammals, one at a time. The pants fit a part of the structure of human beings, but not that of a walrus. Sorry, Wally.

5. Simple filters

Filter documents can be small. For example, there are 48 laureate documents with a value for the field gender equal to female. We can do the same for other fields, like country of death and city of birth. Also, we can merge criteria into a single filter document.

6. Composing filters

This filter document will have the same form as matching documents. In this case, the filter matches Marie Curie. She discovered the element polonium, named after her native land. Poland was under Russian rule at the time. She hoped that naming an element after it would publicize its lack of independence. Many immigrants won Nobel Prizes, but in what proportion? Later exercises in the course will explore this question.

7. Query operators

We've seen filters that match exact values in a document. What about satisfying other constraints? Query operators are like different ways to input values on a website form. Some values you select from options, like countries; some you select with a range slider. All operators on fields wrap around their corresponding values. You might operate a drop-down menu to select a country and a slider to pick a value in a range. Query operators in MongoDB work the same way. You place an operator in a filter document to wrap around a field and its acceptable values.

8. Query operators

For example, let's find documents where the field 'diedCountry' is either France or USA. We use the "in" operator to wrap around acceptable values. Operators in MongoDB have a dollar-sign prefix. Another example. To find documents where 'diedCountry' is not equal to a certain string, we can use the not-equal - or en ee - operator We can compose query operators for a field.

9. Query operators

For example, here we query for documents with 'diedCountry' greater than - or gee tee - the string 'Belgium'. At the same time, we query for 'diedCountry' less than or equal to - or el tee ee - the string 'USA'. How rude that MongoDB considers some countries to be greater than or less than others! Actually, this highlights MongoDB's loose requirements for comparisons. Comparison operators order values in lexicographic order, meaning alphabetical order for non-numeric values. This behavior is something to keep in mind when working with raw, unprocessed data.

10. Let's Practice!

Let's practice constructing filter documents, including those with query operators.