Back to Python Notes
Topic #303

Update Documents

Learn how to modify existing documents in a MongoDB collection using Python's PyMongo library, specifically focusing on the update_one method.

What it is

In document-oriented databases like MongoDB, data is stored as JSON-like documents. Updating a document means changing specific fields within an existing record without replacing the entire document. The mental model is similar to editing a single cell in a spreadsheet row: you identify the row (filter) and specify which column and value to change (update operator).

Key terms include:

  • Filter: A dictionary specifying which document(s) to update.
  • Update Operator: Commands like $set, $inc, or $unset that define how to modify the document.
  • Collection: The group of documents where the update occurs.

Why it matters

  • Data Integrity: Allows precise changes to user profiles, inventory counts, or status flags without risking loss of other data.
  • Efficiency: Updates only the specified fields, reducing network payload compared to replacing whole documents.
  • Atomicity: Single-document updates are atomic, ensuring consistency even under concurrent access.
  • Flexibility: Supports complex operations like incrementing numbers or adding items to arrays directly in the database.

Syntax or steps

The most common method for modifying a single document is collection.update_one(filter, update).

  1. Define the Filter: Create a dictionary matching the unique identifier or criteria of the target document (e.g., {"name": "Ada"}).
  2. Define the Update: Use an update operator. $set is used to assign new values to fields. Example: {"$set": {"age": 37}}.
  3. Execute: Call the method on your collection object.

Example

from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["my_database"]
col = db["users"]

# Initial document might look like: {"name": "Ada", "age": 36, "city": "London"}

# Update Ada's age to 37
result = col.update_one(
    {"name": "Ada"},          # Filter: Find the document where name is "Ada"
    {"$set": {"age": 37}}     # Update: Set the 'age' field to 37
)

print(f"Matched: {result.matched_count}, Modified: {result.modified_count}")

Explanation:

  • MongoClient(...) establishes the connection.
  • col.update_one(...) searches for the first document matching {"name": "Ada"}.
  • {"$set": {"age": 37}} instructs MongoDB to change only the age field to 37, leaving city and other fields untouched.
  • result contains metadata about the operation, such as how many documents were matched and modified.

Common mistakes

  • Missing Operators: Passing {"age": 37} instead of {"$set": {"age": 37}}. Without $set, MongoDB may throw an error or behave unexpectedly depending on context; always use operators for partial updates.
  • Vague Filters: Using a filter that matches multiple documents when you intend to update only one. update_one will only update the first match found, potentially ignoring others. Ensure filters are unique if precision is required.
  • Ignoring Return Values: Not checking matched_count. If the count is 0, the document didn't exist, and no update occurred. Always verify success.
  • Replacing Instead of Updating: Accidentally using replace_one when you meant update_one. This deletes all existing fields not present in the replacement document.

When to use it

Choose between update_one and update_many based on scope.

MethodUse CaseBehavior
update_oneModifying a specific user profile or order status.Updates only the first document matching the filter.
update_manyBulk updates, e.g., marking all expired sessions as inactive.Updates all documents matching the filter.

Practice

Guided Exercise: Write code to increase the score field by 5 for a player named "Bob".

Hint: Use the $inc operator instead of $set.

col.update_one({"name": "Bob"}, {"$inc": {"score": 5}})

Challenge: Update all users in the "New York" city to have a region field set to "Northeast". Check how many documents were modified.

Quick check

Question: What happens if you call update_one with a filter that matches no documents?

Answer: No documents are updated. The result object will show matched_count: 0 and modified_count: 0. No error is raised unless the syntax itself is invalid.

Summary

Updating documents in MongoDB with PyMongo relies on clear filters and specific update operators like $set or $inc. Mastering update_one ensures precise, efficient modifications while preserving unrelated data fields.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Update Documents – FAQs

Quick answers about learning Update Documents in Python.

This free note from Coding Now Tech Institute explains Update Documents in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Update Documents, is 100% free with no signup required.
With focused practice, most students grasp Update Documents in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now