Open In App

Mongoose findOne() Function

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Syntax:

Model.findOne(id)

Parameters:

  • Model: This is the name of the collection used to retrieve the document corresponding to the provided ID.
  • id: This is the identifier for the document you intend to locate.
  • callback`findOne` also accepts a callback function, which can manage errors or perform actions with the retrieved document.

Steps to Installation of Mongoose ModuleMongoose:

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:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"mongoose": "^7.6.5",
}

Example: Below the code example for the findOne() method:

javascript




const mongoose = require('mongoose');
 
// Database connection
    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:

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: Database

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.


Similar Reads

Mongoose Query.prototype.findOne() API
The Mongoose Query API findOne() method is used to find a single document, from a collection, using the MongoDB query system. Syntax: Query.prototype.findOne(filter, projection, options) Parameters: It accepts the following 4 parameters as mentioned above and described below: filter: It is a mongoose object which identifies the existing document to
3 min read
MongoDB - FindOne() Method
The findOne() method finds and returns one document that matches the given selection criteria. If multiple documents satisfy the given query expression, then this method will return the first document according to the natural order which reflects the order of documents on the disk. If no document matches the selection criteria, then this method wil
3 min read
Error: Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
It looks like you're trying to use a method called findOne but it doesn't exist. This error message suggests that there is a circular dependency in your code, which means that two or more modules are trying to import each other. This can cause problems because the modules can't be resolved properly. This error indicates that there is a problem with
3 min read
Mongoose mongoose.model() Function
The mongoose.model() function of the mongoose module is used to create a collection of a particular database of MongoDB. The name of the collection created by the model function is always in plural format mean GFG to gfss and the created collection imposed a definite structure. Syntax: mongoose.model(<Collectionname>, <CollectionSchema>
2 min read
Mongoose | updateMany() Function
The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose
2 min read
Mongoose | findOneAndDelete() Function
The findOneAndDelete() function is used to find a matching document, remove it, and passes the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your mong
2 min read
Mongoose | deleteOne() Function
The deleteOne() function is used to delete the first document that matches the conditions from the collection. It behaves like the remove() function but deletes at most one document regardless of the single option. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command.
2 min read
Mongoose | findByIdAndDelete() Function
The findByIdAndDelete() function is used to find a matching document, removes it, and passing the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your m
2 min read
Mongoose | findByIdAndUpdate() Function
The findByIdAndUpdate() function is used to find a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After
2 min read
Mongoose | findOneAndRemove() Function
The findOneAndRemove() function is used to find the element according to the condition and then remove the first matched element. Installation of mongoose module: You can visit the link to Install mongoose module. You can install this package by using this command. npm install mongoose After installing mongoose module, you can check your mongoose v
2 min read