Search⌘ K
AI Features

Solution: Reading Arrays

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 retrieve documents from the products collection that match certain filter conditions.

  • Line 3: This defines the filter condition for the query.

    • tags: This refers to the array field in each product document.

    • $elemMatch: This checks if any element in the ...