
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In the previous lesson, we learned how to create a model in Mongoose and touched on how to use a basic method to retrieve documents from the database. In this lesson, we will dive deeper into the method and learn how to use it to find documents in Mongoose.
The method is used to retrieve documents from a collection in Mongoose. It is similar to the statement in SQL.
The most basic usage of the method takes no arguments and returns all the documents in the collection. Here is an example:
//
// Find all the documents in a User collection
const users = await User.find();
In this example, the method is called on the model with no arguments. This will return all the documents in the collection.
The method can also be used to filter documents based on certain criteria. You can pass an object as the first argument to the method to specify the criteria for filtering the documents. Here is an example:
//
// Find all the users with the name 'Alice'
const user = await User.find({ name: "Alice" });
The object passed to the method specifies the criteria for filtering the documents. In this example, we are filtering the documents based on the field, and only retrieving the documents where the field is equal to .
We can pass in multiple criteria to the method to further filter the documents. Here is an example:
//
// Find all the users with the name 'Alice' and age 30
const user = await User.find({ name: "Alice", age: 30 });
As long as those fields are present in the document, the method will return the documents that match all the criteria.
The method can also be used to specify which fields to include or exclude from the result. This is known as projection. You can pass a second argument to the method to specify the fields to include or exclude. Here is an example:
//
// Find all the users with the name 'Alice' and age 30
// Include only the name and age fields in the result
const user = await User.find(
{ name: "Alice", age: 30 },
// Include only the name and age fields in the result
"name age",
);
In this example, we are using projection to include only the and fields in the result. Here's an example of the difference in the results:
//
const user = await User.find({ name: "Alice", age: 30 });
//
[
{
_id: "60f7b3b3b3b3b3b3b3b3b3b3",
name: "Alice",
age: 30,
email: "alice@email.com",
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
];
In this next example, we are using projection to include only the and fields in the result. Here's an example of the difference in the results:
//
const user = await User.find({ name: "Alice", age: 30 }, "name age");
//
[
{
name: "Alice",
age: 30,
},
];
This is useful when we have very large documents and only need to retrieve specific fields. Can you think of another scenario where this might be useful?
Using projection is useful in many scenarios
Examples:
Hiding hashed password or other potentially sensitive information from results that might be returned to the client
Retrieving only the fields needed for a specific operation, such as displaying a list of users with their names and email addresses
Improving performance by reducing the amount of data transferred over the network when retrieving large documents
The method also accepts an options object as the third argument. This object can be used to specify additional options for the query, such as sorting, pagination, and even population of referenced documents. Here is an example:
//
// Find all users, but limit the result to 10 documents
const users = await User.find({}, null, { limit: 10 });
That got complicated quickly! Let's break it down:
We can also sort the result by passing a property in the options object. Here is an example:
//
// Find all users, but sort the result by age in descending order
const users = await User.find({}, null, { sort: { age: -1 } });
What's happening here? We're still just focusing on the third argument, the options object. Instead of using the property, we're using the property.
Sort takes in an object where the keys are the fields to sort by, and the values are either for ascending order or for descending order. In this example, we are sorting the result by the field in descending order.
This might look something like this:
//
const users = await User.find({}, null, { sort: { age: -1 } });
//
[
{
name: "Alice",
age: 30,
},
{
name: "Bob",
age: 25,
},
{
name: "Charlie",
age: 20,
},
];
The method is a powerful tool for retrieving documents from a collection in Mongoose. It takes up to three arguments, but you don't always have to use all three. You'll often find yourself only using some of them, depending on your needs.
In this lesson, we learned how to use the method to retrieve documents from a collection in Mongoose. We learned how to filter documents based on certain criteria, use projection to include or exclude fields from the result, and use options to specify additional options for the query.
In the next lesson, we'll cover more complex querying using and learn how to use comparison operators, logical operators, and regular expressions to filter documents.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.