Main Operations
Update

Update

CapybaraDB allows updating documents using operations that are similar to MongoDB. You can update one or multiple documents by specifying filters, update fields, and an optional upsert parameter. This guide explains how to perform update operations.

Update Operation

The update operation is used to update one or multiple documents in a collection based on a filter.

Example Python Code for update

Here’s how you can update documents by applying a filter and an update operation. This example is designed to be similar to the insertion example, showing how you can seamlessly transition from inserting to updating documents in CapybaraDB:

Python
# Filter to match the document(s) based on email (similar to the insert example)
filter_criteria = {
    "email": "johndoe@example.com"
}
 
# Update operation (e.g., updating the 'age' field and modifying bio)
update = {
    "$set": {
        "age": 31,
        "bio": EmbText("John is now an experienced AI specialist with updated skills.")
    }
}
 
# Optional upsert (set to true if you want to insert a new document if no match is found)
upsert = False
 
response = collection.update(update, filter_criteria, upsert)

Update Response

A successful update operation will return a JSON response indicating the number of matched and modified documents, and whether an upsert was performed:

JSON
{
  "matched_count": 1,
  "modified_count": 1,
  "upserted_id": null,
  "task_id": "abc123xyz"
}

If the update object contains EmbJSON data type, the response will include a task_id indicating an asynchronous task is in progress. If no asynchronous processing is required, task_id will be null.

If an upsert was performed, the upserted_id will contain the ID of the new document.

Parameters for Update Operations

ParameterDescription
filterA query object to match the documents to update. This works the same way as MongoDB filters, where you specify conditions to find the documents that need updating. For more details, refer to the filter operator syntax.
updateThe update operations to apply to the matched documents. You can use MongoDB-like update operators such as $set, $inc, etc. For a full list of update operators, refer to the update operator syntax.
upsert(Optional) A boolean flag to create a new document if no documents match the filter. If set to true, a new document will be created using the filter and update information.

How can we improve this documentation?

Got question? Email us