EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB findOne()
 

MongoDB findOne()

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

MongoDB findOne()

 

 

Definition of MongoDB findOne()

MongoDB findOne() function is used to return the single document which satisfied the criteria for the given query. If the given query condition satisfies the criteria in multiple documents then the findOne function in MongoDB will return the first document according to the order in which was document is stored into the disk, basically, as same condition document will return as the natural order. If our collection is caped collection then findOne function returns the data as per insertion order, If our query doesn’t satisfy any condition to retrieve rows from the collection then findOne function will return an empty result.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

  • Below is the syntax of the findOne function in MongoDB.

name_of_collection.findOne (Query, Projection)

findOne (Query, Projection)

Parameter:

Below is the parameter description syntax of the findOne function in MongoDB.

  • Name: This is defined as the name of the collection.
  • FindOne: This is a function that was used in MongoDB to retrieve a single document from the collection.
  • Query: The type of this parameter is a document in MongoDB. This parameter is basically used to specify the criteria of the query using the query operator in MongoDB.
  • Projection: This type of parameter is basically used to specify the return of the field using operators in MongoDB. If we have not used this parameter then it will return all the fields from the matching document.

MongoDB findOne() Function Works

  • Below is the working of the findOne function in MongoDB.
  • Basically, we have using a two-parameter with the findOne function query and the projection.
  • Projection parameter with findOne function will take the document in the following form.

{Collection_field1: <type or boolean>, Collection_field2: <type or boolean>, Collection_field3: <type or boolean>, …, Collection_fieldN: <type or boolean>}

  • Type or boolean is defining as the include or exclude the values from the document in MongoDB.
  • True is defined with value as 1 or true. Basically, the findOne function in MongoDB will specify the _id field if we don’t specify the parameter of projection with the findOne function.
  • False is defined with value as o or false.
  • Projection argument in findOne function will do not mix the exclude and include specifications with the _id exception field.
  • MongoDB findOne function basically returns the single document but it will not return the cursor from the collection.
  • In MongoDB findOne function if we have used projection parameter then findOne function will return the field which was used in projection.
  • The below example shows that if we have used the projection parameter then the findOne function will return the field which was used in the projection.
  • In first example we have used the findOne function without specifying the projection parameter, it will return all the fields from the specified documents.
  • In the second example, we have specified the projection with the findOne function after specifying the projection it will return only the field which was we have specified in the query.

db.inventory.findOne()
db.inventory.findOne({ },{item:1,qty:1})

MongoDB findOne()-1.1

  • If the collection document contains multiple matching fields from the collections findOne the function will return the first documents from the collection.
  • The below example shows that if the collection document contains multiple matching fields from the collections findOne function will return the first documents from the collection are as follows.

db.inventory.find()
db.inventory.findOne({"status" : /^A/ })

MongoDB findOne()-1.1

  • In the above example query, we have specified query which return the documents which contain status as A, it will return only one documents from the collection using the findOne function in MongoDB.
  • In the above example findOne function we have retrieved the first document which contains the status as A.

Examples of MongoDB findOne()

  • Below is an example of the findOne function in MongoDB. We have using inventory collection to describe an example of the findOne function in MongoDB is as follows.
  • Below is the inventory collection document.

db.inventory.find()

MongoDB findOne()-1.2

  • FindOne function with an empty parameter 

The below example shows that findOne function with an empty parameter list.

db.inventory.findOne()

MongoDB findOne()-1.2...

  • FindOne Function with Query Parameter

The below example shows that findOne function with query parameters. We have defined query as return first documents which contain the quantity as 25.

db.inventory.findOne({"qty" : 25 })

MongoDB findOne()-1.3....

  • FindOne Function to Exclude Specified Field

The below example shows that findOne function to exclude the specified field from the document. We have excluded the item and qty field from the specified documents.

db.inventory.findOne({ "status": "A" },{item: 0, qty:0 } );

MongoDB findOne()-1.4....

  • FindOne Function with Projection Parameter

The below example shows that findOne function with projection parameter.

db.inventory.findOne({ },{status:1,qty:1})

MongoDB findOne()-1.6

Recommended Articles

This is a guide to MongoDB findOne(). Here we also discuss the syntax and parameters of mongodb findone() along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB sort()
  2. MongoDB Import
  3. MongoDB Monitoring
  4. MongoDB Relationships

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW