Skip to content
Related Articles

Related Articles

MongoDB – FindOne() Method
  • Last Updated : 05 Feb, 2021

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 will return null. It takes two parameters first one is the query criteria and the other is optional.

  • If you want to find fields of the embedded document, then use the following syntax:

“field.nestedfieldname”: <value>

or

{field: {nestedfieldname: <value>}}

  • The document return by this method always contains the _id field. If you don’t want the _id field, then set _id:0 in the projection.

Syntax:



db.Collection_Name.findOne(

    query:<document>,

    projection:<document>

)

Parameters:

  • The first parameter is a query criteria on the collection.
  • Other is optional.

Optional parameters:

projection: The projection parameter determines which fields are returned to the matching documents. The projection parameter takes a document that contains field : value pairs:

{field1: <value1>, field: <value2>…}



Here,

  • <field>: <1 or true>: It means we specify the inclusion of a field.
  • <field>: <0 or false>: It means we specify the exclusion of a field.
  • “<field>.$”: <1 or true>: Here, by using $ array projection operator we specify the projection to return the first element that matches the given query expression on the array field.
  • <field>: <array projection>: Here, by using array projection operators we specify the array items to include, by excluding those items that do not match the given expression. 
  • <field>: <$meta expression>: Here, by using $meta expression operator we specify the inclusion of available per-document metadata. 
  • <field>: <aggregation expression>: It means we specify the value of the projection field.

Return:

It returns one matched document with the specified query criteria in the collection. And if you specify a projection parameter, then is method returns a document that only contains projection fields.

Examples:

In the following examples, we are working with:

Database: gfg

Collections: student

Document: Three documents contains name and the language in which they are interested.

  • With empty query specification it returns the first document in the collection:
db.student.findOne()

  • Return first document that contains the specified field:
db.student.findOne({language:"python"})

  • Return first document that contains language field:
db.student.findOne({language:"c++"})

  • Finding document using projection:
db.student.findOne({name: "Avinash"}, {_id: 0, language:1})

Here, we find a document whose name is “Avinash”, and only want to display the language that Avinash know. So, in the projection document, we set the value of the language field to 1 and the value of _id to 0.

My Personal Notes arrow_drop_up
Recommended Articles
Page :