mongoose Like Operator

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

Introduction

The LIKE operator is common is SQL. It is a very useful operator when we need to filter the data according to some text. 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 use mongoose for a LIKE operator equivalent.

This is the restaurant collection.

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" }

In each document, there is a field named dish. There are different types of dishes in this collection. Many of these are chicken dishes, such a chicken burger, bacon chicken pasta, chicken salad, etc. Now suppose we want all those dishes that contain chicken in them. We need to filter the result in such a way that each dish containing chicken in its name should be present. In SQL, we would have to use the LIKE operator, but in mongoose, we will use another approach.

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

LIKE in mongoose

We will use regex here. But first, let’s create a route handler.

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

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

Now, we will add a find() method inside it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
router.route('/find').get(function(req,res){

    restuarant.find( {},function(err, result){

        if(err){
            res.send(err)
        }
        else{
            res.json(result)
        }



})

As of now, it will fetch all the documents in the restaurant collection. But we only need those documents in which the dish field contains “chicken” in it. As mentioned earlier, we will use regex. The regex is used in the following way.

1
/chicken/;

The string we need to find is placed between two slashes. Let’s add this to the find() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
router.route('/find').get(function(req,res){

    restuarant.find( {"dish": /chicken/},function(err, result){

        if(err){
            res.send(err)
        }
        else{
            res.json(result)
        }



})

This is how we do it in the query.

1
{"dish": /chicken/}

Now, this route handler will filter all those documents in which the dish field contains “chicken” in it. It does not matter if the “chicken” comes in front, or middle, or at the last. Let’s execute it using the postman tool and see the results.

Image from Gyazo

We can see, it returns only those documents that have chicken in their dishes. Let’s discuss one more example. This time, we will fetch all the documents in which, the dish field contains “bacon”.

Similarly, we will add bacon instead of chicken in the query.

1
{"dish": /bacon/}

Let’s add this to the route handler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
router.route('/find').get(function(req,res){

    restuarant.find( {"dish": /bacon/},function(err, result){

        if(err){
            res.send(err)
        }
        else{
            res.json(result)
        }



})

Let’s execute it using the postman tool.

Image from Gyazo

Yes! It works perfectly.

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.