MongoDB findOne

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

The find method is used to retrieve data from a MongoDB collection. By default, the find method will return all the documents in a collection. If the query and projection are provided, the data will be filtered accordingly and only those documents will be retrieved that match the query. But there is one more method that can be used to retrieve only one document in the collection. This is known as the findOne method. In this article, we will discuss how to use the findOne method in MongoDB.

The findOne method

We have a database (demoDB) and a collection (details) with a few documents. Let’s see how many documents are there in the details collection.

1
2
3
4
5
> db.details.find()
{ "_id" : ObjectId("5d6cf0902b61d6ce41d5a132"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5d6cf09b2b61d6ce41d5a133"), "name" : "Sam", "age" : 22, "location" : "Texas" }
{ "_id" : ObjectId("5d6cf0aa2b61d6ce41d5a134"), "name" : "Lisa", "age" : 24, "location" : "New York" }
>

There are three documents in the details collection. We used the find method without any query, so it returned all the documents present in the collection. What do you think will the findOne method return? Let’s check.

1
2
3
4
5
6
7
8
> db.details.findOne()
{
        "_id" : ObjectId("5d6cf0902b61d6ce41d5a132"),
        "name" : "John",
        "age" : 21,
        "location" : "New York"
}
>

We used the findOne method without any query parameters. It returned the first document of the details collection. This is what the findOne method does. It returns the first matching document and when there is no query given, it will return the very first document on the collection.

So basically, the findOne method returns only a single document. What if a query is given and there is more than one document in the collection that matches the query? Will the findOne method return all the matching documents? No! it won’t. It will only return the very first document that matches the query and all other will be ignored.

1
2
3
4
5
6
7
8
> db.details.findOne({"location" : "New York"})
{
        "_id" : ObjectId("5d6cf0902b61d6ce41d5a132"),
        "name" : "John",
        "age" : 21,
        "location" : "New York"
}
>

In the findOne method, we provided a query – {“location” : “New York”}. Now in our details collection, there are two documents where the location field is New York. But the findOne returns only the first matched document, the second one is ignored.

We can provide projection along with the query in the findOne method.

1
2
3
> db.details.findOne({"location" : "New York"}, {_id: 0, name:1})
{ "name" : "John" }
>

Difference between the find method and the findOne method

  1. With no query given, the find method returns all the documents present in the collection while the findOne returns only the first document.
  2. With query give, the find method returns all the matched documents while the findOne method only returns the first matched document.
  3. If the query does not match any document, the findOne method returns null while the find method never returns null. Observe the following example.
1
2
3
4
> db.details.findOne({"location" : "Chicago"})
null
> db.details.find({"location" : "Chicago"})
>

In both the methods, a query is passed – {“location” : “Chicago”}. But there is no document where location equals Chicago. The findOne method returns null while the find does not. The find method here returns an empty cursor.

Conclusion

We have showed you a few examples of how to use the findOne method. You use it when you only want the first document of a collection that matches the query or if you just want the first document in the collection.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.