Chuck's Academy

Database

READ Operations in MongoDB

In this chapter, we will learn how to perform READ operations in MongoDB, which involves querying documents within collections. The main command to perform reads in MongoDB is find(), which allows us to search for and retrieve documents that match certain criteria.

Retrieving All Documents

The simplest use of find() is to retrieve all documents in a collection. If we want to get all users from the users collection, we use the following command:

javascript
"This command selects and returns all documents in the users collection."

MongoDB returns documents in JSON format, and the find() command without parameters returns all records in the collection. This may not be efficient when the collection contains a large number of documents, so it's often better to use filters.

Filtering Results with find()

To filter results and obtain only documents that meet certain criteria, we can pass a query object to the find() method. Here is an example where we search for users who are over 25 years old:

javascript
"This command selects documents in the users collection where the age field is greater than 25, using the $gt comparison operator."

Comparison Operators

MongoDB provides several operators for comparing values within documents:

  • $gt: Greater than
  • $lt: Less than
  • $gte: Greater than or equal to
  • $lte: Less than or equal to
  • $eq: Equal to
  • $ne: Not equal to

Below is an example of how to combine comparison operators:

javascript
"Here, we are selecting users whose age is between 18 and 30, using the $gte and $lt operators."

Field Projection

In some queries, we only need to obtain certain fields from the documents. MongoDB allows us to specify which fields we want to include or exclude in the results using a projection. Here is an example where we want to get only the names and emails of users:

javascript
"In this example, we are selecting only the name and email fields and excluding the _id field from the results."

Including and Excluding Fields

  • Include a field: We specify the field with a value of 1 in the projection object.
  • Exclude a field: We specify the field with a value of 0.

It is not possible to mix field inclusion and exclusion in the same query, except for the _id field.

Sorting Results

MongoDB allows us to sort query results using the sort() method. Here is an example where we sort users by age in descending order:

javascript
"In this case, we are sorting the results by age, from highest to lowest, using the -1 value in the age field."
  • 1: Ascending order.
  • -1: Descending order.

Limiting and Paginating Results

When working with large volumes of data, it is useful to limit the number of results we get. MongoDB allows you to limit results with limit() and skip a specific number of documents with skip(), which is useful for implementing pagination.

Limiting Results

javascript
"This command returns only the first five documents of the users collection."

Pagination

To paginate results, we can use a combination of limit() and skip(). Here is an example of how to get the second page of results with five documents per page:

javascript
"In this example, we are skipping the first five documents and retrieving the next five, thus implementing pagination."

Logical Operators in Queries

We can combine multiple conditions in a query using logical operators like $and, $or, and $not. For example, to get users who are over 25 years old or live in New York, we can use the following command:

javascript
"This command selects users who are over 25 years old or live in New York City, using the $or logical operator."

Example with $and

If we wanted to combine conditions with $and, the command would be:

javascript
"This example selects only users who meet both conditions, that is, they are over 25 years old and live in New York."

Summary

In this chapter, we have learned how to perform read operations in MongoDB using the find() method. We saw how to filter results, project fields, sort, limit, and paginate results, as well as how to use logical operators in our queries. In the next chapter, we will explore how to update documents in MongoDB using the update() method.


Ask me anything