The $push operator adds a new item to the end of an array inside a document. It helps you keep lists updated easily.
$push operator for adding to arrays in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
db.collection.updateOne(
{ <filter> },
{ $push: { <arrayField>: <valueToAdd> } }
)db.collection.updateOne() updates one document matching the filter.
$push adds the valueToAdd to the end of the array in arrayField.
db.users.updateOne(
{ name: "Alice" },
{ $push: { hobbies: "painting" } }
)db.users.updateOne(
{ name: "Bob" },
{ $push: { scores: 95 } }
)db.users.updateOne(
{ name: "Carol" },
{ $push: { tags: { $each: ["new", "featured"] } } }
)$position.db.users.updateOne(
{ name: "Dave" },
{ $push: { tasks: { $each: ["task1"], $position: 0 } } }
)This program creates a user named Emma with two hobbies. Then it adds a new hobby "cycling" to her hobbies array using $push. It prints the document before and after the update to show the change.
use testdb // Insert a sample user document db.users.insertOne({ name: "Emma", hobbies: ["reading", "swimming"] }) // Show document before update print("Before update:") printjson(db.users.findOne({ name: "Emma" })) // Add a new hobby using $push const updateResult = db.users.updateOne( { name: "Emma" }, { $push: { hobbies: "cycling" } } ) // Show update result print("Update result:") printjson(updateResult) // Show document after update print("After update:") printjson(db.users.findOne({ name: "Emma" }))
The $push operation takes O(1) time to add an element at the end of the array.
Use $each with $push to add multiple items at once.
Common mistake: Trying to $push to a field that is not an array will cause an error.
Use $push when you want to add items to an array; use $addToSet if you want to avoid duplicates.
$push adds new items to the end of an array inside a document.
It is useful for updating lists like hobbies, tags, or tasks.
You can add one or many items using $push and $each.
Practice
$push operator do in MongoDB?Solution
Step 1: Understand the purpose of
The$push$pushoperator is used to add elements to an existing array field inside a MongoDB document.Step 2: Compare with other options
Options A, C, and D describe different operations: removing elements, replacing arrays, or creating collections, which are not what$pushdoes.Final Answer:
Adds a new element to the end of an array field in a document -> Option AQuick Check:
$pushadds elements [OK]
- Confusing $push with $pull (which removes items)
- Thinking $push replaces the whole array
- Mixing $push with collection creation commands
numbers in a document where _id is 1?Solution
Step 1: Identify correct
The correct syntax uses an object with the field name as key and the value to add as value: {$push: {field: value}}.$pushsyntaxStep 2: Analyze each option
db.collection.updateOne({_id: 1}, {$push: {numbers: 5}}) matches the correct syntax. db.collection.updateOne({_id: 1}, {$push: numbers: 5}) misses curly braces around the field-value pair. db.collection.updateOne({_id: 1}, {$push: {numbers => 5}}) uses an invalid arrow syntax. db.collection.updateOne({_id: 1}, {$push: {numbers: [5]}}) pushes the array [5] as a single element instead of adding the scalar value 5.Final Answer:
db.collection.updateOne({_id: 1}, {$push: {numbers: 5}}) -> Option AQuick Check:
Correct $push syntax uses {$push: {field: value}} [OK]
- Omitting braces around field-value pair
- Using wrong symbols like => instead of :
- Pushing an array when a single value is intended
{ _id: 1, tags: ["red", "blue"] }, what will be the tags array after running db.collection.updateOne({_id: 1}, {$push: {tags: "green"}})?Solution
Step 1: Understand
The$pusheffect on array$pushoperator adds the new element to the end of the existing array.Step 2: Apply
Starting with ["red", "blue"], pushing "green" adds it at the end, resulting in ["red", "blue", "green"].$pushto the given arrayFinal Answer:
["red", "blue", "green"] -> Option DQuick Check:
New item added at array end [OK]
- Assuming $push adds at the start
- Replacing the whole array instead of adding
- Confusing order of elements after push
colors array in a document with _id: 2. Which update command will NOT work correctly?Solution
Step 1: Understand how to push multiple items
To add multiple items to an array,$pushmust be combined with$eachinside the update document.Step 2: Analyze each option
Options B, C, and D correctly use$eachto add multiple values. db.collection.updateOne({_id: 2}, {$push: {colors: ["yellow", "purple"]}}) tries to push an array directly, which will add the entire array as a single element, not multiple elements.Final Answer:
db.collection.updateOne({_id: 2}, {$push: {colors: ["yellow", "purple"]}}) -> Option BQuick Check:
Use $each to push multiple items [OK]
- Pushing an array directly instead of using $each
- Forgetting curly braces around $each
- Using wrong operators like $addToSet instead of $push
{ _id: 3, tasks: ["task1"] }. You want to add "task2" and "task3" only if they are not already in the array. Which update command correctly achieves this?Solution
Step 1: Understand difference between $push and $addToSet
$pushadds items regardless of duplicates.$addToSetadds items only if they don't exist already.Step 2: Analyze the commands
db.collection.updateOne({_id: 3}, {$addToSet: {tasks: { $each: ["task2", "task3"] }}}) uses$addToSetwith$eachto add multiple unique items. Options A and D use$pushwhich can add duplicates. db.collection.updateOne({_id: 3}, {$push: {tasks: { $addToSet: ["task2", "task3"] }}}) incorrectly nests$addToSetinside$push, which is invalid syntax.Final Answer:
db.collection.updateOne({_id: 3}, {$addToSet: {tasks: { $each: ["task2", "task3"] }}}) -> Option CQuick Check:
Use $addToSet with $each for unique additions [OK]
- Using $push when duplicates should be avoided
- Nesting $addToSet inside $push incorrectly
- Forgetting $each when adding multiple items
