Learn

In addition to querying array fields for exact matches and individual elements, we can use comparison operators to search for documents where elements in an array field meet some condition or range of conditions.

For example, imagine we have a collection of tennis athletes, called tennis_players, with each document having a similar structure:

{ _id: ObjectId(...), name: "Serena Williams", country: "United States", wimbledon_singles_wins: [2002, 2003, 2009, 2010, 2012, 2015, 2016] }

If we wanted to search this collection for all athletes who have been Wimbledon Singles Champions from the year 2000 onward, we could run the following query:

db.tennis_players.find({ wimbledon_singles_wins: { $gte: 2000 } });

This would return all documents where the wimbledon_singles_wins array has at least one element with a value of 2000 or greater. Our query result might look something like this:

{ _id: ObjectId(...), name: "Serena Williams", country: "United States", wimbledon_singles_wins: [2002, 2003, 2009, 2010, 2012, 2015, 2016] }, { _id: ObjectId(...), name: "Venus Williams", country: "United States", wimbledon_singles_wins: [2000, 2001, 2005, 2007, 2008] }, { _id: ObjectId(...), name: "Roger Federer", country: "Switzerland", wimbledon_singles_wins: [2003, 2004, 2005, 2006, 2007, 2009, 2012, 2017] },

We can also query based on compound conditions. Let’s consider that we want to search our tennis_players collection to find all athletes who won a Wimbledon Singles Championship either before 1935, in the first 50 years of the championship, or after 1971, in the 50 most recent years of the tournament. We could achieve this with the following query:

db.tennis_players.find({ wimbledon_singles_wins: { $gt: 1971, $lt: 1935 } })

This might return the following set of documents:

{ _id: ObjectId(...), name: "Suzanna Lenglen", country: "United States", wimbledon_singles_wins: [1919, 1920, 1921, 1922, 1923, 1925] }, { _id: ObjectId(...), name: "Roger Federer", country: "Switzerland", wimbledon_singles_wins: [2003, 2004, 2005, 2006, 2007, 2009, 2012, 2017] }, …

Note that this query would match documents where the array contains elements that satisfy the query conditions in some combination. One element could satisfy the greater than 1971 condition, while another could satisfy the less than 1935 condition. And if the ranges overlapped, a single element could satisfy both conditions. However, using this syntax, it is not necessary that a single element satisfies all conditions.

In the next exercise we’ll learn how to filter our queries such that the matching documents have at least one array element that satisfies all the specified criteria.

Before moving on, let’s practice querying with comparison operators on compound filter conditions!

Instructions

1.

Connect to the restaurants database. Then, search the listingsAndReviews collection for restaurants that received a Michelin star after the year 2010.

2.

In the same listingsAndReviews collection, search for restaurants that have a michelin_stars array field containing values that are greater than 2015 or less than 2010.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?