$all in mongoose

Introduction

Arrays are one of the most commonly used data structures in the programming world. They are perfect to store elements of the same data types. We can store any type of data in arrays. While working with databases, we may need to store data in an array. For example, the following MongoDB documents contain a field that stores an array of Strings.

1
2
3
{
    "locations" : ["New York" , "Texas", "Chicago"]
}

Mongoose provides quite a few methods and operators to work with arrays. One such operator is $all operator. The $all operator is useful while filtering data. In this article, we will discuss how to use $all operators in mongoose.

We will use the details collection.

1
2
3
{ "_id" : ObjectId("5dff8d81099480835a114b5c"), "name" : "John", "age" : 21, "locations" : [ "New York", "Chicago" ] }
{ "_id" : ObjectId("5dff8d8f099480835a114b5d"), "name" : "Sam", "age" : 21, "locations" : [ "New York", "Chicago", "Texas" ] }
{ "_id" : ObjectId("5dff8fc8099480835a114b5e"), "name" : "Lisa", "age" : 25, "locations" : [ "Washington DC" ] }

We will use the postman tool for testing. You can download the postman tool from www.getpostman.com.

$all operator

To understand the $all operator, observe the following.

1
{"locations" : { $all : ["New York", "Texas"]}}

If the above query is provided to a find() method, It will returns all the documents where the locations field contains the two values – “New York” and “Texas”. It does not matter how many more values the locations field contains, It will match if the specified values are present. Similarly, if even one the value is missing, the document will not be matched. So the $all operator can be very useful while retrieving data. Let’s create a route handler and add the above query in the find method.

1
2
3
4
5
6
7
8
9
10
11
12
router.route("/fetchData").get(function(req, res) {
  details.find({ locations: { $all: ["New York", "Texas"] } }, function(
    err,
    result
  ) {
    if (err) {
      res.send(err);
    } else {
      res.send(result);
    }
  });
});

Let’s execute this route using the postman tool and see what happens.

Image from Gyazo

It returns a document in which the locations field contains three values – New York, Chicago, and Texas. But we specified only two of them. The $all operator will match the document if all the specified values are present, even if it contains more values. Similarly, if we provide another value to the $all operator that is not present in any of the locations fields in collection.

1
{"locations":{ $all:  ["New York", "Texas", "Detroit"]}}

Now, some documents contain “New York” and “Texas” in the locations field. But none of them has “Detroit”. Let’s execute and see what happens.

Image from Gyazo

No document matches! This is because all three values are not present in any of the document’s locations field.

Conclusion

The $all operator allows you to check for documents that have an array field with all of the values given. The syntax is very straightforward and is easy to use. The $all operator is also a functionality that comes up very often and so is a very practical operation to have mastered. We hope this simple demo of how to use the $all operator was helpful.

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.