Or in mongoose

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

Introduction

In this article we will talk about how to use the OR operator in our mongoose queries. The or operator is the counterpart to the and operator and is essential knowledge when working with database queries. We’ll specifically show you how to use within mongoose. Please continue reading to learn how to use or in mongoose.

When you might use the or operator

Almost every method in mongoose has a query part. The query is used to match a document. The specified method only works after a document is matched. A query is an object. It may contain any condition, or it can be empty. Observe the following find() method.

1
find({ name: "Rambo" });

The find method will return the document where the value of the name field is “Rambo”. What if we want all the documents where the value of the name field is “Rambo”, and the value of the breed field is “Pugg”?

1
find({ name: "Rambo", breed: "Pugg" });

Do you think this will work? No! The find() method will look for the document where the value of the name field is “Rambo” and the value of the breed field is “Pugg”. But we want all those documents where either the name is “Rambo” and breed is “Pugg”. In such kind of situations, we can use the $or operator. The $or operator will match either of the condition with the documents. It does not matter if all the conditions match or not. The only condition matching the document is enough with the $or operator.

In this article, we will discuss how to use $or operator in mongoose.

We will use the $or operator on the kennel collection.

1
2
3
4
5
{ "_id" : ObjectId("5df390ce4e8d7e9979b2d21d"), "name" : "Rambo", "age" : 5, "breed" : "Pitbull" }
{ "_id" : ObjectId("5df390f34e8d7e9979b2d21e"), "name" : "Scooby Doo", "age" : 2, "breed" : "Great Dane" }
{ "_id" : ObjectId("5df391404e8d7e9979b2d220"), "name" : "Spike", "age" : 3, "breed" : "Pitbull" }
{ "_id" : ObjectId("5df391544e8d7e9979b2d221"), "name" : "Lui", "age" : 2, "breed" : "Pugg" }
{ "_id" : ObjectId("5df49352cfc314ad6829bf5f"), "name" : "Johnny Boy", "age" : 3, "breed" : "Labrador" }

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

$or operator

The following router handler will be invoked when the endpoint ‘/fetch’ will be executed.

1
2
3
router.route('/fetch').get(function(req,res){

})

Suppose, we want all the documents where the value of the name field is “Rambo”, or the value of the breed field is “Pugg”, or the value of the age field is 2. We can use the $or operator. The $or operator takes an array of objects as its value. Each object will contain a condition.

1
$or: [{ name: "Rambo" }, { breed: "Pugg" }, { age: 2 }];

Let’s add this to a find() method.

1
2
3
4
5
6
7
8
9
10
11
12
router.route("/fetch").get(function(req, res) {
  kennel.find(
    { $or: [{ name: "Rambo" }, { breed: "Pugg" }, { age: 2 }] },
    function(err, result) {
      if (err) {
        res.send(err);
      } else {
        res.send(result);
      }
    }
  );
});

Let’s execute it using the postman tool.

Image from Gyazo

It returns all the documents where even only one of the condition matches.

Conclusion

In this knowledge-base article we demonstrated how to use the or operator in mongoose. We explained the circumstances in which you might use the or operator as well as how you would execute it using mongoose. Using these basic operators are essential for anybody working with the database and having a concrete understanding of them is essential.

If you have any database needs please don’t hesitate to reach out to us at Object Rocket.

Below is a code summary of the article showing how to execute an or query in mongoose. It was build using the express framework for NodeJs.

Code Summary

1
2
3
4
5
6
7
8
9
10
11
12
router.route("/fetch").get(function(req, res) {
  kennel.find(
    { $or: [{ name: "Rambo" }, { breed: "Pugg" }, { age: 2 }] },
    function(err, result) {
      if (err) {
        res.send(err);
      } else {
        res.send(result);
      }
    }
  );
});

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.