Search⌘ K
AI Features

Solution: Reading Arrays

The query on the products collection utilizes the find() method to filter documents based on the tags array. It employs $elemMatch to identify any tag containing the word "usb," using a case-insensitive regular expression to ensure accurate matches. The projection specifies which fields to include in the output, omitting the default _id field for clarity. Key concepts include filtering by array contents, using regular expressions for pattern matching, and defining output fields through projection.

We'll cover the following...
Query
db.products.find(
{
tags: { $elemMatch: { $regex: /\busb\b/i } }
},
{
name: 1,
category: 1,
tags: 1,
_id: 0
}
)

The explanation of the query is given below:

  • Line 1: This begins a query on the products collection using the find() method. This tells MongoDB that we want to ...