Skip to content

MongoDB findOne() Explained by Examples AQSA Spark By {Examples}

  • by

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.

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

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 –

projection –

options –

The method returns a single document, 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 findOne() method to retrieve a document by its _id value by following this query.

#Finding document by id using findOne()
db.student.findOne({_id: ObjectId(“642efc803872b0c387af32e0”)})

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

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 MongoDB query is the findOne() method in MongoDB that gives the first document that matches the specified query criteria. The specific criteria set within the findOne() method is that the value of the gender field should be equal to the given string value.

As we have multiple documents in the collection that match this criterion but 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 Fields

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.  

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

4. Find a document and retrieved the next document using findOne() method in MongoDB

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}})

The above query is specified with the findOne() method over the collection student. The findOne() method takes the field marks as an expression which is further set with a search expression. 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

Conversely, 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 inside it. Then, the field name is assigned 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. The output generated the null value because the string value of the name field is not matched with any of the documents.

6. Conclusion

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

More details about this topic can be found here.

 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. 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  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *