Update
OneNode DB 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 use the /document
endpoint via the REST API to perform update operations.
Parameters for Update Operations
Parameter | Description |
---|---|
filter | A 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 official MongoDB documentation on query filters (opens in a new tab). |
update | The 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 official MongoDB documentation on update operators (opens in a new tab). |
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. |
Update Operation
The update
operation is used to update one or multiple documents in a collection based on a filter. The same /document
endpoint is used with a PUT
request to perform both single and multiple document updates.
Endpoint
To update documents, use the following endpoint:
{collection_url}/document
Where {collection_url}
is the full collection URL that includes your db_id
and collection_name
.
Example Python Code for update
Here’s how you can update documents using Python, 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 OneNode DB:
import requests
import json
# API Key and Collection URL (Use environment variables for production purposes)
ONENODE_API_KEY = "your_example_api_key_here"
collection_url = "https://api.onenode.ai/v1/db/123abc/collection/my_collection"
# Document URL for Update
url = f"{collection_url}/document"
# Filter to match the document(s) based on email (similar to the insert example)
filter = {
"email": "johndoe@example.com"
}
# Update operation (e.g., updating the 'age' field and modifying bio)
update = {
"$set": {
"age": 31,
"bio": {
"@embText": {
"text": "John is now an experienced AI specialist with updated skills.",
"model": "text-embedding-3-small"
}
}
}
}
# Optional upsert (set to true if you want to insert a new document if no match is found)
upsert = False
# Request body
data = {
"filter": filter,
"update": update,
"upsert": upsert
}
# Headers with API Key
headers = {
"Authorization": f"Bearer {ONENODE_API_KEY}",
"Content-Type": "application/json"
}
# Sending the request
response = requests.put(url, json=data, headers=headers)
# Print the response
print(response.json())
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:
{
"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.
If you need additional help with the OneNode DB API, feel free to explore our documentation or reach out to support.