The findOne Function in MongoDB: Basic Guide

In this tutorial, I will explain the findOne function in MongoDB. With great features and functionalities, MongoDB is a highly advanced and highly powerful database management system. Beginners often favor MongoDB for its ease of use and powerful capabilities.

A MongoDB application can handle heavy CRUD operations due to the functions it provides. Dashboard integrations, forums, eCommerce platforms, review and listing platforms, and the like are examples of such applications.

MongoDB fits well with popular JavaScript frameworks like NodeJs. It is part and parcel of software or web development to search for, update, delete, or remove data from a dummy database. Additionally, these are operations important to the work of a great many applications.

Using the findOne function, a single document will be found based on the given condition. The first document matching the specified condition is returned if more than one document does.

This guide will demonstrate various examples for you so you can get a better idea of how you can use the findOne function in MongoDB.

The findOne Function Syntax in MongoDB

Let us take a quick look at the findOne function syntax in MongoDB:

db.collection.findOne(query, projection)

Using the findOne Function in MongoDB

Let us get started with using the findOne function in MongoDB.

Finding Document Without Specifications Using the findOne Function

In this example, we will find the first document in our collection without specifying any conditions in the findOne function.

  • Start up the MongoDB service
  • Choose the database you want to use and move into it:
show dbs
use dronesGala

Also read: Pretty Print in MongoDB: What it is & How to Use it? [Ultimate Guide]

  • Before we move ahead, let us take a quick look at all the available documents inside our drones collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
  • Finding the first document in our collection using the findOne function in MongoDB:
> db.drones.findOne()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}

Perfect! The findOne function has spat out the very first document for us.

Finding Document with a Specific Field Value Using the findOne Function

In this example, I will pass a specific field and its value to check in the findOne function in MongoDB.

> db.drones.findOne( {name:`Jade Balestrom RW` } )

{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}

Very well! We have successfully found the first document in our collection that contains the specified field value using the findOne function.

Finding Document with a Specific Nested Document Value Using the findOne Function

In this example, I will pass a specific nested field or document value using the findOne function in MongoDB.

> db.drones.findOne( { "additionalDetails.material": `lithium` } )

{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}

Great! We have successfully received the first document with a specific nested field value or a nested document value using the findOne function in MongoDB.

Finding Document with a Specific Nested Array Field Value Using the findOne Function

In this example, I am passing a specific nested array field value to get the first document that matches that value using the findOne function in MongoDB.

> db.drones.findOne( { "additionalDetails.moreUses": [ `Recreation` ] })

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}

We now have a document in front of us that contains a specific nested array field value spat by the findOne query function.

Finding Document with No Matching Values Using the findOne Function

Lastly, I will pass a value to the findOne query function that exists in none of the documents.

> db.drones.findOne( { "additionalDetails.moreUses": [ `Land Inspection` ] })

null

We simply get the ‘null’ value.

Read More: Basic 2021 Guide to MongoDB findById Function

Conclusion

Learn to use the findOne query function in MongoDB with the afore-cited examples.

Noteworthy References

MongoDB

Aneesha S
Aneesha S
Articles: 172