Update All Elements in a MongoDB Array Using $[] Positional Operator
📖 Scenario: You manage a MongoDB collection that stores information about books in a library. Each book document contains an array of reviews, where each review has a rating and a comment. You want to update all reviews to increase their rating by 1 point.
🎯 Goal: Build a MongoDB update query that uses the $[] all positional operator to increment the rating field of every review in the reviews array for a specific book.
📋 What You'll Learn
Create a collection named books with one document containing a title and a reviews array with exactly two review objects.
Define a filter variable filter to select the book with the exact title 'Learn MongoDB'.
Write an update query using $inc and the $[] all positional operator to increment the rating of all reviews by 1.
Complete the update command by combining the filter and update variables in a db.books.updateOne() call.
💡 Why This Matters
🌍 Real World
Updating all elements in an array field is common when you want to apply the same change to every item, such as increasing ratings, marking all tasks as done, or resetting flags.
💼 Career
Knowing how to use the $[] positional operator is important for database developers and administrators working with MongoDB to efficiently update array data without multiple queries.
Progress0 / 4 steps
1
Create the books collection with one book document
Create a variable called books and assign it an array with one object. The object must have a title field set to 'Learn MongoDB' and a reviews array containing exactly two objects: the first with rating 3 and comment 'Good intro', the second with rating 4 and comment 'Very helpful'.
MongoDB
Hint
Remember to create a list with one dictionary. The dictionary should have the exact keys and values as specified.
2
Define the filter to select the book by title
Create a variable called filter and assign it a dictionary that matches documents where the title is exactly 'Learn MongoDB'.
MongoDB
Hint
Use a dictionary with the key title and the exact string value.
3
Write the update query using $[] to increment all ratings
Create a variable called update and assign it a dictionary that uses $inc with the key 'reviews.$[].rating' set to 1 to increment all rating fields inside the reviews array.
MongoDB
Hint
Use the $inc operator with the key reviews.$[].rating to update all array elements.
4
Complete the update command with db.books.updateOne()
Write a line that calls db.books.updateOne() with the filter and update variables as arguments to update the document.
MongoDB
Hint
Use db.books.updateOne(filter, update) to apply the update.
Practice
(1/5)
1. What does the $[] operator do in a MongoDB update query?
easy
A. It updates every element in an array inside the matched documents.
B. It updates only the first element of an array.
C. It removes all elements from an array.
D. It adds a new element to the array.
Solution
Step 1: Understand the role of $[]
The $[] operator is called the all positional operator and targets all elements in an array during an update.
Step 2: Compare with other array update operators
Unlike $ which updates the first matched element, $[] updates every element in the array.
Final Answer:
It updates every element in an array inside the matched documents. -> Option A
Quick Check:
$[] updates all array elements [OK]
Hint: Remember $[] means update all items in the array [OK]
Common Mistakes:
Confusing $[] with $ which updates only one element
Thinking $[] adds or removes elements
Assuming $[] works outside arrays
2. Which of the following is the correct syntax to increment every element in the scores array by 1 using $[] in MongoDB?
easy
A. db.collection.updateMany({}, { $inc: { 'scores.$[]': 1 } })
B. db.collection.updateMany({}, { $set: { 'scores.$[]': 1 } })
C. db.collection.updateMany({}, { $push: { 'scores.$[]': 1 } })
D. db.collection.updateMany({}, { $inc: { 'scores': 1 } })
Solution
Step 1: Identify the operator to increment array elements
The $inc operator increments numeric values. Using 'scores.$[]' targets all elements in the array.
Step 2: Check syntax correctness
db.collection.updateMany({}, { $inc: { 'scores.$[]': 1 } }) correctly uses $inc with $[] to increment each element by 1. Other options either use wrong operators or wrong field paths.
Hint: Only $inc, $mul, $set work with $[], not $push [OK]
Common Mistakes:
Using $push with $[] causing syntax error
Confusing $inc and $mul usage
Trying to multiply array directly without $[]
5. You have documents with a field items as an array of objects like { _id: 1, items: [{qty: 2}, {qty: 5}] }. You want to add 3 to the qty of every item. Which update query correctly does this?