Update

CapyDB provides a flexible way to update documents in a collection using operations similar to MongoDB's update functionality. You can use various update operators to modify fields, and optionally create new documents if no matches are found (upsert).

Update Operation

The update operation allows you to modify existing documents in a collection that match a specified filter. You can use different update operators to perform various modifications on the matched documents.

Example Python Code for update

Here's how you can update documents using Python, with various update operators and upsert option:

# Filter to match the document(s) to update
filter_criteria = {
    "name": "Alice Smith"
}

# Update operations to apply
update_operations = {
    "$set": {
        "age": 30,
        "email": "alice.smith@example.com"
    },
    "$inc": {
        "login_count": 1
    }
}

# Optional upsert parameter (default: False)
upsert = True

# Sending the request
response = collection.update(filter_criteria, update_operations, upsert)

Update Response

A successful update operation will return a JSON response containing information about the operation's results. Here's an example response:

{
  "matched_count": 1,
  "modified_count": 1,
  "upserted_id": null
}

Parameters for Update Operations

ParameterDescription
filterA query object to match the documents to update. This works the same way as MongoDB filters, allowing you to specify conditions to find the documents. For more details, refer to the filter operator syntax.
updateAn object containing update operators that specify the modifications to apply. For more details, refer to the update operator syntax.
upsert (optional)A boolean value that, when set to true, creates a new document if no documents match the filter criteria. Defaults to false.

How can we improve this documentation?

Got question? Email us