Search⌘ K
AI Features

Solution: Updating Arrays

We'll cover the following...
Query
db.products.updateMany(
{ price: { $lte: 0 } },
{
$set: {
price: 49,
status: "Inactive"
},
$addToSet: {
tags: "needs-review"
}
}
)
, db.products.find({status: "Inactive"})

Explanation

The explanation of the query is given below:

  • Line 1: db.products.updateMany; this command tells MongoDB to update multiple documents inside the products collection that match a given condition.

  • Line 2: It selects all products whose price is less than or equal to zero, meaning they are incorrectly priced or invalid entries. ...