EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB findOne()
Secondary Sidebar
MongoDB Tutorial
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove

MongoDB findOne()

By Priya PedamkarPriya Pedamkar

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more