The find() method in Mongoose is used to query documents from a collection. It returns a Mongoose Query object, which supports chaining additional operations like sorting, limiting, and filtering.
Syntax:
Model.find(conditions, [projection], [options], [callback]) In the above syntax:
- conditions: Specifies the query conditions (like filters) to match documents.
- projection: Optional. Select which fields to include or exclude from the documents.
- options: Additional options such as sorting or limiting the number of results.
- callback: Optional function to execute when the query is completed.
Return Value
- Returns a Mongoose Query object.
- When executed, it returns an array of documents matching the conditions.
How find() Works in Mongoose
- The
find()function executes a query and returns documents that match the given conditions. - It supports query projection to include or exclude specific fields from the results.
- It also allows you to limit the number of results or sort them by specific fields, offering fine-grained control over your data retrieval process.
Example: Using the Mongoose find() Method
// Filename: index.js
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
// Only one parameter [query/condition]
// Find all documents that matches the
// condition name='Punit'
User.find({ name: 'Punit'}, function (err, docs) {
if (err){
console.log(err);
}
else{
console.log("First function call : ", docs);
}
});
// Only Two parameters [condition, query projection]
// Here age:0 means don't include age field in result
User.find({ name: 'Punit'}, {age:0}, function (err, docs) {
if (err){
console.log(err);
}
else{
console.log("Second function call : ", docs);
}
});
// All three parameter [condition, query projection,
// general query options]
// Fetch first two records whose age >= 10
// Second parameter is null i.e. no projections
// Third parameter is limit:2 i.e. fetch
// only first 2 records
User.find({ age: {$gte:10}}, null, {limit:2}, function (err, docs) {
if (err){
console.log(err);
}
else{
console.log("Third function call : ", docs);
}
});
In this example:
- 'mongoose.connect()': Establishes a connection to the MongoDB database using the provided URI.
- 'UserSchema': Defines the schema for the
Usermodel withnameandagefields. - 'User': It represents the Mongoose model for the collection.
- 'User.find()': Executes a query on the
Usercollection to find documents matching the given conditions. We can use additional options to filter, limit, or project specific fields.
Below is the sample data in the database before the find() function is executed, We can use any GUI tool or terminal to see the database, like we have used Robo3T GUI tool as shown below:

Step 4: Run the Application
To start the application run the following command.
node index.js
Use Cases for Mongoose find()
- Fetching all documents: Retrieve all documents that match the query condition, useful for displaying lists or datasets.
- Excluding fields: Sometimes you may want to exclude sensitive data like passwords. The
find()function allows you to do that through projection. - Limiting results: To prevent performance issues with large datasets, you can limit the number of records returned by the
find()function using thelimitoption.
Common Query Options with find()
limit: Restricts the number of documents returned.skip: Skips a certain number of documents (useful for pagination).sort: Sorts the results by a specified field (e.g.,{ age: 1 }for ascending order).select: Specifies which fields to include or exclude in the result (projection).