Introduction

Welcome back to our MongoDB course! By now, you've become adept at querying with basic operators, logical conditions, and element queries. Today, we delve into array query selectors—an extremely powerful feature to simplify your data querying process in MongoDB. In this lesson, we'll explore the $size, $all, and $elemMatch array query selectors using our Comic Book Store database.

Array Query Selectors at a Glance

Array Query Selectors in MongoDB comprise a set of operators designed to handle queries specific to array fields. These selectors include:

  • $size: Finds documents where an array contains a specified number of elements.
  • $all: Finds documents where an array contains all the specified elements.
  • $elemMatch: Finds documents containing an array element that matches all specified criteria.
Exploring `$size`

The $size query selector is used to find documents where an array field has a specific number of elements. For example, to find a comic book that is related to exactly three other comics in our database, we use the $size operator with the value of 3:

This query searches for the first document in the comic_books collection where the related_comics array contains exactly three elements. The projection part { title: 1, related_comics: 1, _id: 0 } ensures that only the title and related_comics fields are returned, excluding the _id field.

Better Querying with `$all`

The $all query selector is used to find documents where an array field contains all the specified elements. For instance, if we want to find a comic book that falls under both 'Action' and 'Superhero' genres, we can use the $all operator:

This query looks for the first document in the comic_books collection that contains both 'Action' and 'Superhero' in its genres array.

Understanding `$elemMatch`

Imagine that you want to find a comic book where one of the characters is named Iron Man with the alter ego Bruce Banner. Given that Iron Man's alter ego is Tony Stark, this search should return no results, right?

Let's execute the query:

Unexpectedly, it returns The Avengers comic book. What's the problem? The problem is that this query finds a comic book where there is a character named Iron Man and there is a character with the alter ego Bruce Banner (not necessarily the same character).

To write the correct query, you can use the $elemMatch operator, which will find a comic book that has a character matching all specified criteria at once:

In this query, we search for a document in the comic_books collection where the characters array contains an object with name set to "Spider-Man" and alter_ego set to "Peter Parker".

Summary

You've now explored the $size, $all, and $elemMatch array query selectors in MongoDB. We've covered the syntax and use cases for each to help you streamline your database queries. Up next are the practice exercises where you can apply these concepts and hone your skills. Keep practicing to become proficient in MongoDB querying!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal