Updated March 4, 2023
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:
- 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})
- 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/ })
- 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()
- FindOne function with an empty parameter
The below example shows that findOne function with an empty parameter list.
db.inventory.findOne()
- 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 })
- 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 } );
- FindOne Function with Projection Parameter
The below example shows that findOne function with projection parameter.
db.inventory.findOne({ },{status:1,qty:1})
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 –