Mongoose findOne with Multiple Conditions

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

Introduction

The findOne() method is one of the two popular methods that are used to retrieve data from MongoDB collections in mongoose. As the name suggests, the findOne() methods find a single document only. Usually, we provide a query to match a document. The findOne() method also works fine if an empty object is passed to it. It will return the very first document in this case. We can provide as many conditions in the query, but it does not matter how many documents can match the query, only the first match document will be returned. In this article, we will discuss how to use Mongoose findOne with multiple conditions.

We will use the details collection.

1
2
3
4
5
{ "_id" : ObjectId("5dc44ffe54a67437ca1aafab"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5dc4501a54a67437ca1aafac"), "name" : "Sam", "age" : 25, "location" : "Chicago" }
{ "_id" : ObjectId("5dc4502c54a67437ca1aafad"), "name" : "Lisa", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5dc4503a54a67437ca1aafae"), "name" : "Max", "age" : 31, "location" : "Detroit" }
{ "_id" : ObjectId("5dc4504954a67437ca1aafaf"), "name" : "Sunny", "age" : 28, "location" : "Texas" }

For performing HTTP endpoint testing, we will use the postman tool. You can download the postman tool from www.getpostman.com.

findOne() with multiple conditions

Go through all the documents in the details collection. The first and third document has the same values for the age and location fields. Let’s use the findOne() method and we will use these two fields as conditions.

The following route handler will be invoked when the endpoint ‘/find’ will be executed.

1
router.route("/find").get(function(req, res) {});

Let’s insert the findOne() method with multiple conditions.

1
2
3
4
5
6
7
details.findOne({ age: 21, location: "New York" }, function(err, result) {
  if (err) {
    res.send(err);
  } else {
    res.send(result);
  }
});

There are two conditions in the query.

1
{"age" : 21, "location" : "New York"}

Now, we have two documents that can match these conditions – one with the name “John”, and the second with the name “Lisa”. Let’s execute this route using the postman tool and see what happens.

Image from Gyazo

As mentioned earlier, the findOne() method returns only one document. It returned the document where the name is “John” because it was matched first. This is how the findOne() method works with multiple conditions. We can provide as many conditions we want but it will return only the first matched document.

But remember, all the conditions should match. Even if one condition out of all conditions fails to match, it will not return anything.

Conclusion

Thank you for joining us for this tutorial on how to use the Mongoose findOne method with multiple conditions. We hope you can apply what you’ve learned to your specific problem.

If you ever need expert database help please don’t hesitate to reach out to us.

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.