Mongoose findOne() Function
Last Updated :
13 Aug, 2024
The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.
findOne in Mongoose
findOne() is used when you want to retrieve a single document from a collection based on a specific condition, such as finding a user by their email or username.
Syntax:
Model.findOne(query, [projection], [options], [callback])
Parameters:
Model: This is the name of the collection used to retrieve the document corresponding to the provided ID.- query: An object specifying the conditions that the returned document(s) must match.
- projection (optional): Specifies which fields to include or exclude from the returned document.
- options (optional): Additional options, such as sorting or limiting the results.
callback: findOne also accepts a callback function, which can manage errors or perform actions with the retrieved document.
Features
- Efficiently fetches a single document that matches the criteria.
- Supports complex queries with multiple conditions.
- Allows you to specify which fields to include or exclude in the returned document.
- Works seamlessly with promises and async/await for cleaner code.
Steps to Install Mongoose Module:
Step 1: You can visit the link Install Mongoose module. You can install this package by using this command.
npm install mongoose
Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm version mongoose
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"mongoose": "^7.6.5",
}Example: This example connects to a MongoDB database using Mongoose and uses the findOne() function to retrieve a single user document where the age is greater than or equal to 5.
JavaScript
// 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 }
});
// Find only one document matching
// the condition(age >= 5)
// Model.findOne no longer accepts a callback
User.findOne({age: {$gte:5} })
.then((docs)=>{
console.log("Result :",docs);
})
.catch((err)=>{
console.log(err);
});
Steps to run the program: Run the following command in the terminal
node index.js
Below is the sample data in the database before the function is executed. You can use any GUI tool or terminal to see the database like we have used the Robo3T GUI tool as shown below: 
Console Output:

So this is how you can use the mongoose findOne() function that finds one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.
Summary
The findOne() function in Mongoose is a powerful and efficient method for retrieving a single document from a MongoDB collection. It allows for flexible querying, field projection, and integrates well with promises, making it an essential tool for working with MongoDB in Node.js applications.