• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:9 mins read
You are currently viewing MongoDB findOne() Explained by Examples

This article will cover about db.collection.findOne() method in MongoDB. In MongoDB, a single document that matches a particular query can be retrieved from a collection using the findOne() method.

Advertisements

In this article, we will explain more about the findOne() method in MongoDB and provide examples to demonstrate its usage. Let’s consider the collection student, we will use this collection to explain the usage of findOne() method.


# Create Collection
db.student.insertMany([
      {  
         name: "Harry",
         age: 24,
         gender: "Male",
         marks: 80
 
      },
      {
         name: "Candice",
         age: 20,
         gender: "Female",
         marks: 89
      },
      {
         name: "Marrie",
         age: 25,
         gender: "Female",
         marks: 85
      },
      {
         name: "Harry",
         age: 24,
         gender: "Male",
         marks: 90
      }
   ]
)

1. MongoDB findOne() Method Usage

As the name implies, the MongoDB db.collection.findOne() method is used to retrieve one document from the collection or view where it satisfies the specified query criteria. If the query returns multiple documents, it still returns one document which would be the first document based on the order of the query result.

The findOne() also provides an optional argument that specifies what fields should be present on the returned document.

Returns null if no document is retrieved for the query criteria.

1.1 Syntax of findOne()

Following is the syntax of the db.collection.findOne() method.


# Syntax of findOne()
db.collection.findOne(query, projection, options)

1.2 Paraters & Return

The parameters are

  • query – Specify the query criteria using this optional parameter.
  • projection – Specify fields to return in the result using the optional projection parameter.
  • options – Optional options that are used to modify the query and the fields to return.

The method returns a single document from the collection, when no document is found it returns a null.

2. MongoDB findOne() by using _id

As we know that the _id field of each document in a collection is unique. So, we can use MongoDB findOne() method to retrieve a document by its _id value by following this query.


# Use findOne() to get a document
db.student.findOne({_id: ObjectId("642efc803872b0c387af32e0")})

The above query of the findOne() method takes the _id field with a value of a specific objectId. The findOne() method return a single document from the student collection based on its unique _id value. The output shows the document matched the given objectId value.

MongoDB findOne()

3. Using findOne() by field

Alternatively, we can use findOne() to retrieve the first document that matches a specific query by any field in the collection.


# Finding single document having specified field 
db.student.findOne({gender: "Female"})

The above findOne() method in MongoDB gives the first document that matches the gender field should be equal to the given string value query criteria.

As we have multiple documents in the collection that match this criteria, the findOne() method will return the first matching document. We can see the output below that displayed the single document with the matched field value.

3. Find a Single Document with Multiple Conditions

In addition, we can also call findOne() to retrieve a document that matches multiple conditions. Let’s take the following query of the findOne() method with more than one field to match.


# findOne() method with multiple fields
db.student.findOne({name: "Harry", age: 24})

Here the findOne() method is set with two fields name and age with their values. It searches a single document in the student collection where both the name and age fields are equal to the specified values, respectively. The findOne() method returns the very first document that fulfills the provided search condition.

Hence, the output is retrieved which is displayed in the single-matched document below.  

findOne() method in MongoDB

You can also find objects between two dates in MongoDB using this link.

4. Using findOne() with Conditional Operator

The conditional operator can also be utilized in the findOne() method to retrieve the single document which satisfied the given condition to the specified field. Here, the $gt operator is a comparison operator that chooses documents where the marks field value is greater than the predetermined value.


# findOne() method with conditional operator
db.student.findOne({marks: {$gt: 85}})

Here, the search criteria matched with many documents but the findOne() method only returns the document which appeared first in the collection. That document is yielded in the below output.

5. Find a single document by the field value not exists

When a non-existence field value is passed to the findOne() method then the null value is returned. This is explained through the following query.


# findOne() method with  non-existence field value
db.student.findOne({name: "Mark"})

The above query of the findOne() method takes the name field with the string value which doesn’t exist in any of the documents of the student collection. If no documents match the given expression then findOne() will return null.

findOne() method in MongoDB

6. Conclusion

In conclusion, findOne() provides a flexible and efficient way to retrieve one document from the MongoDB collection. All the above examples of the findOne() method help us to understand the usage of the method in MongoDB.

More details about this topic can be found here.