
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 covered some basic usage of the method in Mongoose. In this lesson we will cover some more advanced methods that can be used to find documents in Mongoose.
We'll cover:
There are many more find methods- to see the full list of methods, check out the Mongoose documentation
as well as more complex querying using and learn how to use advanced filtering techniques like:
The method is used to find a single document in the database that matches the specified query. It returns the first document that matches the query.
//
// Find the first Post with the title 'Hello World'
const doc = await Post.findOne({ title: "Hello World" });
//
{
_id: '60f3b3b3b9b3f3b3b3b3b3b3',
title: 'Hello World',
content: 'This is my first post',
createdAt: '2024-04-23T12:00:00.000Z',
updatedAt: '2024-04-23T12:00:00.000Z'
}
The method returns a single document that matches the query. If no document is found, it returns .
Note that this method of documents, even if multiple documents match the query. If you need to find multiple documents, use the method instead.
The method is used to find a single document by its field. It returns the document that matches the specified .
//
// Find the Post with the mongoDB _id
const doc = await Post.findById("60f3b3b3b9b3f3b3b3b3b3b3");
//
{
_id: '60f3b3b3b9b3f3b3b3b3b3b3',
title: 'Hello World',
content: 'This is my first post',
createdAt: '2024-04-23T12:00:00.000Z',
updatedAt: '2024-04-23T12:00:00.000Z'
}
Mongoose provides a set of comparison operators that can be used to filter documents based on specific conditions. These operators can be used with the , , and methods.
Here are some of the most commonly used comparison operators:
: Matches values that are equal to a specified value.
: Matches values that are greater than a specified value.
: Matches values that are greater than or equal to a specified value.
: Matches values that are less than a specified value.
: Matches values that are less than or equal to a specified value.
: Matches any of the values specified in an array.
There are many more- including versions of these operators. For a full list of comparison operators, check out the Mongoose documentation
To use these operators- you can add them into our first argument of the method.
//
// Find all Posts with a like count greater than 10
const docs = await Post.find({ likes: { $gt: 10 } });
//
[
{
_id: "60f3b3b3b9b3f3b3b3b3b3b3",
title: "Hello World",
content: "This is my first post",
likes: 15,
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
{
_id: "60f3b3b3b9b3f3b3b3b3b3b4",
title: "Hello World 2",
content: "This is my second post",
likes: 20,
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
];
Here, instead of passing in an exact value for the field, we are using the operator to find all posts with a like count greater than 10. We're the value of the field to the value 10. That's why they're called .
You can use the operator to find all posts with a like count less than 10.
//
// Find all Posts with a like count less than 10
const docs = await Post.find({ likes: { $lt: 10 } });
In the above list, all of those operators essentially do the same thing- they compare the value of the field to the value specified in the operator. The only difference is the type of comparison that is being made.
The operator is a bit different- it checks if the value of the field is in the array of values specified in the operator.
This is incredibly useful for filtering documents based on specific conditions. You can use these operators to filter documents based on a wide range of conditions, such as dates, numbers, and strings.
For example, we can use the operator to find all posts with a category of either 'Technology' or 'Science':
//
// Find all Posts with a category of 'Technology' or 'Science'
const categoriesToFind = ["Technology", "Science"];
const docs = await Post.find({ category: { $in: categoriesToFind } });
//
[
{
_id: "60f3b3b3b9b3f3b3b3b3b3b3",
title: "Hello World",
content: "This is my first post",
category: "Technology",
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
{
_id: "60f3b3b3b9b3f3b3b3b3b3b4",
title: "Hello World 2",
content: "This is my second post",
category: "Science",
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
];
The operator is used to match any of the values specified in an array. It doesn't have to be an array of strings- it can be an array of any type of value.
Mongoose also provides a set of logical operators that can be used to combine multiple conditions in a single query. These operators can be used with the , , and methods.
Here are some of the most commonly used logical operators:
: Matches documents that satisfy all the specified conditions.
: Matches documents that satisfy at least one of the specified conditions.
This is how we would filter documents based on using the operator:
//
// Posts with a like count greater than 10 and a category of 'Technology'
const docs = await Post.find({
$and: [{ likes: { $gt: 10 } }, { category: "Technology" }],
});
//
[
{
_id: "60f3b3b3b9b3f3b3b3b3b3b3",
title: "Hello World",
content: "This is my first post",
likes: 15,
category: "Technology",
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
];
In the above example, we are using the operator to find all posts with a like count greater than 10 and a category of 'Technology'. The operator is used to combine multiple conditions in a single query.
It takes an of conditions as its value, and each condition is an object that specifies the field and the comparison operator.
We can pass as many conditions as we want to the operator. All of the conditions must be satisfied for a document to be included in the result set.
You can use the operator in the same way we used to find all posts with a like count greater than 10 or a category of 'Science'.
//
// Find all Posts with a like count greater
// than 10 or a category of 'Science'
const docs = await Post.find({
$or: [{ likes: { $gt: 10 } }, { category: "Science" }],
});
The operator is used to match documents that satisfy at least one of the specified conditions. It takes an of conditions as its value, and each condition is an object that specifies the field and the comparison operator.
Mongoose also supports the use of regular expressions to filter documents based on patterns in the field values. Regular expressions are a powerful tool for matching text patterns in strings.
This is similar to how you would use regular expressions in JavaScript. If you're not familiar with regular expressions, you can learn more about them in the MDN Web Docs.
Here's an example of how you can use regular expressions to find all posts with a title that starts with 'Hello':
//
// Find all Posts with a title that starts with 'Hello'
const docs = await Post.find({ title: /^Hello/ });
//
[
{
_id: "60f3b3b3b9b3f3b3b3b3b3b3",
title: "Hello World",
content: "This is my first post",
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
{
_id: "60f3b3b3b9b3f3b3b3b3b3b4",
title: "Hello My Friends",
content: "This is my second post",
createdAt: "2024-04-23T12:00:00.000Z",
updatedAt: "2024-04-23T12:00:00.000Z",
},
];
In the above you can see that we are using the character to match the start of the string. This regular expression will match any string that starts with 'Hello', regardless of what comes after it. There are a lot of different regular expression patterns you can use to match different types of strings- and we can't get into all of it here. But knowing you have the ability to use regular expressions in Mongoose is a powerful tool.
In this lesson, we covered some more advanced methods for finding documents in Mongoose. We learned how to use the and methods to find single documents by query and respectively.
We also learned how to use comparison operators to filter documents based on specific conditions, logical operators to combine multiple conditions in a single query, and regular expressions to match text patterns in strings.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.