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).
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.
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)
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
}
Parameter | Description |
---|---|
filter | A 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. |
update | An 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 . |
Your feedback helps us improve our documentation. Let us know what you think!