Article image Querying documents in MongoDB: Using the findOne() method

9.3. Querying documents in MongoDB: Using the findOne() method

Page 29 | Listen in audio

MongoDB is an open-source NoSQL database that provides high-performance support, high availability, and easy scalability. He works on the concept of collections and documents. In this chapter, we will discuss how to query documents from a collection in MongoDB.

One of the most common operations you will need to perform on a MongoDB database is document querying. Querying is the process of retrieving documents from a collection. MongoDB provides a number of methods to accomplish this task, one of which is the findOne() method.

The findOne() method is used to return a single document that meets the specified criteria. If multiple documents match the criteria, this method returns the first document according to the natural order, which reflects the order of documents on disk.

The basic syntax of the findOne() method is as follows:

db.collection.findOne(query, projection)

Where:

  • db is the current database.
  • collection is the collection in which you are looking for the document.
  • query is a document that specifies search conditions. This is an optional parameter.
  • projection is a document that specifies the fields to be returned in the resulting document. This is an optional parameter.

Let's assume we have the following collection of documents:

{
    "_id": 1,
    "name": "Maria",
    "age": 25,
    "profession": "Engineer"
},
{
    "_id": 2,
    "name": "John",
    "age": 30,
    "profession": "Doctor"
},
{
    "_id": 3,
    "name": "Ana",
    "age": 35,
    "profession": "Lawyer"
}

If we want to find the document where the "name" field is "Maria", we can use the findOne() method as follows:

db.collection.findOne({"name": "Maria"})

The result will be the first document that matches the query:

{
    "_id": 1,
    "name": "Maria",
    "age": 25,
    "profession": "Engineer"
}

Now, let's say we want to return only the "profession" field in the resulting document. We can do this by specifying the "profession" field in the projection parameter:

db.collection.findOne({"name": "Maria"}, {"profession": 1})

The result will be:

{
    "_id": 1,
    "profession": "Engineer"
}

Note that the "_id" field is always returned unless you explicitly exclude it.

The findOne() method is extremely useful for retrieving a single document from a collection. However, remember that it returns the first document that matches the query. If you need to retrieve all documents that match the query, you should use the find() method.

In summary, MongoDB provides a variety of methods for querying documents, each with its own uses and advantages. The findOne() method is just one of these methods, but it is one of the most commonly used due to its simplicity and effectiveness.

Now answer the exercise about the content:

Which MongoDB method is used to return a single document that meets the specified criteria, and if multiple documents match the criteria, returns the first document according to the natural order?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Querying documents in MongoDB: Querying documents using query operators

Next page of the Free Ebook:

30Querying documents in MongoDB: Querying documents using query operators

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text