mongoose Query LIKE

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

Introduction

In SQL, we have the LIKE operator that is used to match a pattern in a string. It is very useful while performing CRUD operations on a database. Unfortunately, no such operator exists in MongoDB. But it does not mean we cannot achieve the functionality of the LIKE operator in MongoDB and mongoose. In this article, we will discuss how to use a mongoose query LIKE equivalient.

We will use the restaurant collection for the demonstration.

1
2
3
4
5
6
7
8
9
10
11
12
{ "_id" : ObjectId("5e2e86c8ee1f22706dac168f"), "dish" : "chicken burger", "price" : "5" }
{ "_id" : ObjectId("5e2e86e1ee1f22706dac1690"), "dish" : "bacon burger", "price" : "7" }
{ "_id" : ObjectId("5e2e8716ee1f22706dac1691"), "dish" : "bacon chicken pasta", "price" : "10" }
{ "_id" : ObjectId("5e2e8721ee1f22706dac1692"), "dish" : "noodles in chicken", "price" : "10" }
{ "_id" : ObjectId("5e2e8777ee1f22706dac1697"), "dish" : "chicken salad", "price" : "10" }
{ "_id" : ObjectId("5e2e8781ee1f22706dac1698"), "dish" : "bacon salad", "price" : "12" }
{ "_id" : ObjectId("5e2e8799ee1f22706dac1699"), "dish" : "tuna sandwich", "price" : "15" }
{ "_id" : ObjectId("5e2e87a4ee1f22706dac169a"), "dish" : "chicken sandwich", "price" : "12" }
{ "_id" : ObjectId("5e2e87dfee1f22706dac169b"), "dish" : "red sauce pasta", "price" : "5" }
{ "_id" : ObjectId("5e2e87efee1f22706dac169c"), "dish" : "red sauce chicken pasta", "price" : "10" }
{ "_id" : ObjectId("5e2e87f9ee1f22706dac169d"), "dish" : "red sauce bacon pasta", "price" : "12" }
{ "_id" : ObjectId("5e2e8810ee1f22706dac169e"), "dish" : "super veggie salad", "price" : "7" }

Each document contains three fields – dish and price, and the autogenerated _id field of course. There are different types of dishes in this collection. Many of these are bacon dishes, such a bacon burger, bacon chicken pasta, bacon salad, etc. Now suppose we want all those dishes that contain bacon in them. We need to filter the result in such a way that each dish containing bacon in its name should be present. We have the LIKE operator for such cases in SQL. But in mongoose, we have to follow another approach. We will be using the regular expression in a query.

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

LIKE in query

So let’s start by creating a route handler.

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

The above route handler will be invoked when the endpoint ‘/like’ is executed. So as mentioned earlier, we will use regex. But first, let’s add a find() method in it.

1
2
3
4
5
6
7
8
9
router.route("/like").get(function(req, res) {
  restaurant.find({}, function(err, result) {
    if (err) {
      res.send(err);
    } else {
      res.json(result);
    }
  });
});

Currently, we are passing just an empty object as a query. This will return all those documents in the collection. But we only need those documents in which the dish field contains “bacon” in it. We need to use regex. We do it in the following way.

1
/bacon/;

We will use it in the following way in the query.

1
{ "dish" : /bacon/}

This will match all those documents that contain “bacon” in the dish. It does not matter if “bacon” is at the beginning, or middle, or end. Let’s add this to the find() method.

1
2
3
4
5
6
7
8
9
router.route("/like").get(function(req, res) {
  restaurant.find({ dish: /bacon/ }, function(err, result) {
    if (err) {
      res.send(err);
    } else {
      res.json(result);
    }
  });
});

Image from Gyazo

Yes! It works just like the LIKE operator. It returns only those documents that have bacon in their dishes.

Conclusion

So we do not have a LIKE operator in MongoDB. But MongoDB is very flexible and there is almost always a way to do something that is present in SQL and not in MongoDB. The regex is perfect to achieve the functionality of the LIKE operator in mongoose.

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.