Learn

In the last exercise, we learned how to use MongoDB’s .find() command to query all documents in a collection. However, what if we wanted to find a specific set of data in our collection? If we are looking for a specific document or set of documents, we can pass a query to the .find() method as its first argument (inside of the parenthesis ( )). With the query argument, we can list selection criteria, and only return documents in the collection that match those specifications.

The query argument is formatted as a document with field-value pairs that we want to match. Have a look at the example syntax below:

db.<collection>.find( { <field>: <value>, <second_field>: <value> ... } );

We can have as many field-value pairs as we want in our query! To see the query in action, consider the following collection (shortened for brevity) of automobile makers in a collection named auto_makers:

{ maker: "Honda", country: "Japan", models: [ { name: "Accord" }, { name: "Civic" }, { name: "Pilot }, … ] }, { maker: "Toyota", country: "Japan", models: [ { name: "4Runner" }, { name: "Corolla" }, { name: "Rav4" }, … ] }, { maker: "Ford", country: "USA", models: [ { name: "F-150" }, { name: "Bronco"}, { name: "Escape"}, … ] }

Imagine we wanted to query this collection to find all of the vehicles that are manufactured in "Japan". We could use the .find() command with a query, like so:

db.auto_makers.find({ country: "Japan" });

This would output the following documents from our collection:

{ maker: "Honda", country: "Japan", models: [ { name: "Accord" }, { name: "Civic" }, { name: "Pilot }, … ] }, { maker: "Toyota", country: "Japan", models: [ { name: "4Runner" }, { name: "Corolla" }, { name: "Rav4" }, … ] }

Note: Query fields and their associated values are case and space sensitive. So, a query for a value "Corolla" would not be valid for a lowercase version like "corolla". This also applies if we accidentally included spaces. So, " corolla" would also not be valid if the value was "corolla".

Under the hood, find() is actually using an operator to find matches to our query. Operators are special syntax that specifies some logical action we want to perform when our method executes. In the case of the .find() method, it uses the implicit equality operator, $eq, to match documents that include the specified field and value.

If we wanted to explicitly include the equality operator in our query document, we could do so with the following field-value pair:

{ <field>: { $eq: <value> } }

This is the equivalent of using the format seen in the first example:

{ <field>: <value> }

Fortunately, MongoDB handles implicit equality for us, so we can simply use the shorthand syntax for basic queries. In the upcoming exercises, we’ll learn about other operators that we can use to specify ranges and other criteria for matching documents in our queries.

Let’s practice using .find() to do some basic querying on our restaurants database!

Instructions

1.

You’re visiting Brooklyn, New York, and want to find a place for lunch. Thankfully, we have a database full of restaurants!

Connect to the restaurants collection, and then query the listingsAndReviews collection to find a list of restaurants in the borough "Brooklyn".

2.

Wow! There are so many options! Narrow down your results by querying the listingsAndReviews collection again, this time where the borough is "Brooklyn" and cuisine is "Caribbean".

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?