Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Upsert Behavior (Update or Insert) in MongoDB
📖 Scenario: You are managing a small online bookstore's inventory using MongoDB. You want to keep track of books and their stock counts. Sometimes you receive new books, and sometimes you get more copies of existing books. You want to write a query that updates the stock count if the book already exists, or inserts a new book if it does not.
🎯 Goal: Build a MongoDB query that uses upsert behavior to update the stock count of a book if it exists, or insert a new book document if it does not.
📋 What You'll Learn
Create a collection named books with initial book documents.
Define a filter to find a book by its title.
Write an updateOne query with upsert: true option.
Update the stock field or insert a new book document.
💡 Why This Matters
🌍 Real World
Inventory management systems often need to update existing records or add new ones seamlessly. Upsert queries help keep data accurate without extra checks.
💼 Career
Database developers and backend engineers use upsert operations to efficiently maintain data integrity and reduce code complexity.
Progress0 / 4 steps
1
Create the initial books collection with two book documents
Create a variable called books and assign it an array with two objects. The first object should have title as "The Great Gatsby" and stock as 3. The second object should have title as "1984" and stock as 5.
MongoDB
Hint
Use a list of dictionaries with exact keys and values as shown.
2
Define a filter to find a book by its title
Create a variable called filter and assign it an object with the key title and the value "The Great Gatsby".
MongoDB
Hint
The filter is a dictionary with the key title and the exact value.
3
Write an updateOne query with upsert: true option to update the stock
Create a variable called update and assign it an object with the key $set and value an object that sets stock to 10.
MongoDB
Hint
Use the $set operator to update the stock field.
4
Complete the upsert operation with updateOne and upsert: true
Create a variable called options and assign it an object with upsert set to true. Then create a variable called result and assign it the call to db.books.updateOne(filter, update, options).
MongoDB
Hint
Use the upsert: true option to insert if the book does not exist.
Practice
(1/5)
1. What does the upsert option do in a MongoDB updateOne operation?
easy
A. It only inserts a new document without updating existing ones.
B. It updates a document if it exists, or inserts a new one if it does not.
C. It deletes a document if it exists, otherwise does nothing.
D. It duplicates the document regardless of existence.
Solution
Step 1: Understand the upsert option
The upsert option in MongoDB means update if found, insert if not found.
Step 2: Apply to updateOne operation
When using updateOne with upsert: true, MongoDB updates the matching document or inserts a new one if none matches.
Final Answer:
It updates a document if it exists, or inserts a new one if it does not. -> Option B
Quick Check:
Upsert = update or insert [OK]
Hint: Upsert means update if found, else insert new [OK]
Common Mistakes:
Thinking upsert only inserts without updating
Confusing upsert with delete operation
Assuming upsert duplicates documents
2. Which of the following is the correct syntax to perform an upsert using updateOne in MongoDB?
easy
A. db.collection.updateOne(filter, update, {update: true})
B. db.collection.updateOne(filter, update, {insert: true})
C. db.collection.updateOne(filter, update, {upsert: true})
D. db.collection.updateOne(filter, update, {replace: true})
Solution
Step 1: Recall the updateOne method parameters
The updateOne method takes a filter, an update document, and an options object.
Step 2: Identify the correct option for upsert
The option to enable upsert is {upsert: true}, which tells MongoDB to insert if no match is found.
Final Answer:
db.collection.updateOne(filter, update, {upsert: true}) -> Option C
Quick Check:
Use upsert: true in options [OK]
Hint: Use {upsert: true} in updateOne options [OK]
Common Mistakes:
Using {insert: true} instead of {upsert: true}
Omitting the options object entirely
Confusing update with replace option
3. Given the collection users with documents: { _id: 1, name: "Alice", age: 25 } What will be the result after running:
A. The filter syntax is incorrect and causes a syntax error.
B. MongoDB does not support upsert with updateOne.
C. The update document is missing the $set operator.
D. Missing the upsert: true option in the updateOne call.
Solution
Step 1: Check the updateOne parameters
The updateOne call lacks the options object with upsert: true, so it only updates existing documents.
Step 2: Confirm upsert behavior
Without upsert: true, no new document is inserted if the filter finds no match.
Final Answer:
Missing the upsert: true option in the updateOne call. -> Option D
Quick Check:
Upsert option needed to insert new docs [OK]
Hint: Add {upsert: true} to updateOne options to insert [OK]
Common Mistakes:
Forgetting to add upsert option
Confusing missing $set with upsert behavior
Believing updateOne cannot upsert
5. You want to update the status field to "active" for a user with email: "user@example.com". If no such user exists, insert a new document with email and status. Which MongoDB command correctly achieves this?