Visit complete MongoDB roadmap

← Back to Topics List

$all

The $all operator is used to match arrays that contain all specified elements. This allows you to filter documents based on multiple values in a single array field.

Syntax

The basic syntax for using the $all operator is:

{
  <field>: {
    $all: [<value1>, <value2>, ..., <valueN>]
  }
}

Here, <field> refers to the name of the array field that should be queried, and <value1>, <value2>, ..., <valueN> are the values that you want to match against.

Example

Let’s assume we have a collection movies with documents containing the following fields: _id, title, and tags. The tags field is an array of string values.

Here is an example document from the movies collection:

{
  _id: 1,
  title: "The Matrix",
  tags: ["action", "sci-fi", "cyberpunk"]
}

If you want to find all movies with the tags “action” and “sci-fi”, you can use the $all operator as shown below:

db.movies.find({ tags: { $all: ['action', 'sci-fi'] } });

This query would return all documents where the tags array contains both “action” and “sci-fi” values.

Summary

The $all operator allows you to match documents based on the presence of multiple values in an array field. It provides a simple and powerful way to query for documents that meet specific criteria within arrays.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.