I am going to update a document using upsert_item function of CosmosDB Python SDK.
Here is a script:
from dotenv import load_dotenv
from azure.cosmos import CosmosClient
import os
import uuid
def update():
load_dotenv()
# Initialize the Cosmos DB client
endpoint = os.getenv("COSMOSDB_ENDPOINT")
key = os.getenv("COSMOSDB_KEY")
client = CosmosClient(endpoint, key)
# Specify your database and container (collection) names
database_name = os.getenv("COSMOSDB_DB")
container_name = os.getenv("COSMOSDB_CONTAINER")
# Retrieve an item by its ID
container = client.get_database_client(database_name).get_container_client(container_name)
id = str(uuid.uuid4())
url = "https://google.com/"
container.create_item(
{
"id": id,
"status": "pending", # => started, analyzing, finished
"url": url,
"categories": ["7149b375-8cb2-4180-ae03-27fd0da409d0"],
"doctype": "url"
}
)
query_text = f"SELECT * FROM c WHERE c.id='{id}'"
query_items_response = container.query_items(query=query_text, enable_cross_partition_query=True)
data = list(query_items_response)
print(len(data)) # returns 1
for item in data:
print(f"Updating {item['url']}")
item["categories"] = []
item["doctype"] = "pdfDocument"
container.upsert_item(item)
updated_query_items_response = container.query_items(query=query_text, enable_cross_partition_query=True)
updated_data = list(updated_query_items_response)
print(updated_data)
print(len(updated_data)) # returns 0
# Confirm created data using CosmosDB data explorer.
if __name__ == "__main__":
update()
After creating a document, I updated it using the upsert_item method. Sometimes, when I query the data by its ID, I can see the document.
However, in reality, it has been removed. Even after waiting for some time, I am unable to retrieve the document, both through the code and in the Cosmos DB Data Explorer.
And I found a strange problem.
If I change item["doctype"] = "pdfDocument" to item["doctype"] = "site" or others, it works as expected.
It is quite strange.
1Updating https://google.com/[]0Sometimes it returns1Updating https://google.com/[{...}]1, but I cannot see the document in CosmosDB data explorer.itemprior to callingupsert_item()? Aside from that:upsert_item()doesn't delete documents.itemprior to callingupsert_item(). It doesn't have any problems.create_itemandquery_itemsfunction correctly. However,upsert_itemdoes not behave as expected. If I had used the wrong SDK, the other methods in the code would also likely fail to work.