Learn

In the world of databases, persistence describes a database’s ability to store data that is stable and enduring. There are four essential functions that a persistent database must be able to perform: create new data entries, and read, update and delete existing entries. We can summarize these four operations with the acronym CRUD.

In this lesson, we’ll focus on the R in CRUD, reading data. So - how exactly do we start to read data from our MongoDB database? Well, in order to read data, we must first query the database. Querying is the process by which we request data from the database. The most common way to query data in MongoDB is to use the .find() method. Let’s take a look at the syntax:

db.<collection>.find()

Notice the .find() method must be called on a specific collection. When we call .find() without arguments, it will match all of the documents in the specified collection. If our query is successful, MongoDB will return a cursor, an object that points to the documents matched by our query. Because our queries could potentially match large numbers of documents, MongoDB uses cursors to return our results in batches.

In other words, when we query collections using the .find() method, MongoDB will return up to the first set of matching documents. If we want to see the next batch of documents, we use the it keyword (short for iterate).

Now, let’s practice using the .find() method!

Instructions

1.

Inside the restaurants database, there is a collection called listingsAndReviews.

Connect to the restaurants database, and then query the listingsAndReviews collection to get familiar with the documents it stores.

2.

The cursor only returned the first batch of documents. Iterate through the cursor to see the next batch of documents.

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?