MongoDB

How to use $all operator in MongoDB

MongoDB is a NoSQL database that provides extensive support of operator classes to help in retrieving data. The $all operator falls under the category of an array operator class. As the name of $all (all operators in MongoDB) indicates, it is used to get the document from a database collection if it matches all the values in an array field. Moreover, the $all operator also provides support to match nested arrays if present in any field.

In this article, a brief insight into usage of $all operator in the context in MongoDB is provided.

How $all operator works in MongoDB

As discussed above, with the help of $all operators; one can fetch documents based on array fields.

For better understandings, let’s have a look at the syntax of $all operator:

{"field": {$all: ["value1", "value2"...]}}

This operator searches for the specified values and any document that has a field with exact values, is retrieved. However, it is noticed that $all will work only if all the values match the values of an array field in a document. The working mechanism of $all relates with $and (logical operator in MongoDB); both operators look for exact matches. But $and operator can be used with several data types whereas $all is only specific to array data type fields.

How $all operator works in MongoDB

In this guide, following MongoDB instances will be used:

  • MongoDB database: The MongoDB database used in this guide is named as “linuxhint
  • Collection: We have associated “projects” collection with “linuxhint” database,

The following documents resides in “projects” collection:

> db.projects.find().pretty()

Example 1: Basic usage of $all operator

This example demonstrates the fundamental usage of $all operators; For instance, the command mentioned below will look for exact match of array values in “managers” field; only those documents are displayed that have the manager’s names “Mike” and “Sam“:

> db.projects.find({managers: {$all: ["Mike", "Sam"]}}).pretty()

Example 2: Using $all operator with nested arrays

If the document contains nested arrays as in our case “hardware” project contains a nested array of managers, we can get the document by specifying the nest array in $all operator. The below mentioned command will fetch the document that have managers “Alen“, “Sam” and “Elon“:

> db.projects.find({managers: {$all: [["Alen", "Sam"], "Elon"]}}).pretty()

It is noticed that if you want to use only a nested portion of the array; you can also do so, and the following command will help you in this regard:

> db.projects.find({managers: {$all: [["Alen", "Sam"]]}}).pretty()

Example 3: Using $all operator to match a value

Apart from dealing with arrays, the use of $all operators can be extended to match the values in the document. In our case, the command mentioned below will get those documents that have “cost” value equals to “5000“:

> db.projects.find({cost: {$all: [5000]}}).pretty()

Or one can say that, the command written below will also provide you the same result:

> db.projects.find({cost: 5000}).pretty()

> db.mycollection.find().pretty()

Conclusion

MongoDB provides an extensive list of operators that are used to retrieve the required documents from the collection of any Mongo database. In this article, an array associated operator named $all is discussed briefly in the MongoDB context. This operator can be used to match the array values in a field and fetch that relevant document. Other than array values, $all also provides support to fetch the document by matching any value (other than an array).

About the author

Adnan Shabbir