Point operations refer to the CRUD (Create, Read, Update, Delete) operations that can be performed on a single item/document in a container. The operations are referred to as ‘point’ since you’re specifically targeting an item/document in a container – a point in your data graph. Cosmos DB offers rich API support for these point operations.
Operational Aspects: Create, Update and Delete Documents
Create Documents
Documents in Cosmos DB are nothing but JSON objects. In .NET SDK v3 for Cosmos DB, you can utilize the CreateItemAsync
method that creates a new item. Below is a sample code snippet.
ItemResponse
id = Guid.NewGuid().ToString(), name = "Sample Name", description = "Sample Description"});
In the example above, YourClass
is a class that matches the JSON you are writing to the database.
Update Documents
Updating a document in Cosmos DB requires the knowledge of three parameters:
- id: Identifies which document should be updated.
- partition key: For partitioned containers, a partition key value must be specified.
- the updated document: The document with the updated fields.
Below is a basic example of how to update a document.
var updatedItem = new YourClass { id = "some-id", name = "Updated Name", description = "Updated Description"};
ItemResponse
Delete Documents
Deleting a document is straightforward. By using DeleteItemAsync
method, you can remove any document. Here, you must remember to specify both the id
of the document and the partitionKey
value, if applicable.
await container.DeleteItemAsync
Performance Aspects
Cosmos DB offers three key advantages on point operations. They are:
- Atomicity: Every operation is preserved and is executed in its entirety.
- Consistency: The consistency level can be guaranteed for each operation.
- High performance: Cosmos DB guarantees less than 10 ms latency at the 99th percentile for point reads and writes.
The performance can be maximized by suitably partitioning data and leveraging Cosmos DB’s query execution mechanics, so make sure to go through Microsoft’s detailed documentation on these topics.
In conclusion, Azure Cosmos DB offers a versatile and powerful platform for managing documents through point operations. Systematically utilizing them allows users to create, update, and delete documents efficiently, enabling faster data manipulation and better application performance.
Practice Test
True or False: Point operations in Microsoft Azure Cosmos DB are always single item operations.
- True
- False
Answer: True
Explanation: Point operations in Microsoft Azure Cosmos DB are always operations that act on a single item, such as creating, reading, deleting, or replacing an item.
In Microsoft Azure Cosmos DB, how would you create a document using a point operation?
- A. Use the CreateDocumentAsync method
- B. Use the UpdateDocumentAsync method
- C. Use the DeleteDocumentAsync method
- D. None of the above
Answer: A. Use the CreateDocumentAsync method
Explanation: The CreateDocumentAsync method is used to create a new document in a collection in Cosmos DB.
True or False: To update a document in Cosmos DB, the developer is needed to submit an entire updated document.
- True
- False
Answer: True
Explanation: While updating a document in Cosmos DB using a point operation, the entire updated document must be submitted, not just the changes that were made.
Which method is used to delete a document in Microsoft Azure Cosmos DB?
- A. DeleteDatabaseAsync
- B. DeleteDocumentAsync
- C. DeleteContainerAsync
- D. None of the above
Answer: B. DeleteDocumentAsync
Explanation: The DeleteDocumentAsync method is used to delete a document in a collection in Cosmos DB.
True or False: Point operations in Azure Cosmos DB are always transactional.
- True
- False
Answer: True
Explanation: Points operations in Azure Cosmos DB are always transactional, meaning that they either completely succeed or fail.
Which of the following HTTP status codes is returned when a
document is successfully created in Azure Cosmos DB?
- A. 200
- B. 201
- C. 204
- D. 404
Answer: B. 201
Explanation: When a document is successfully created in Azure Cosmos DB, it returns HTTP status code
True or False: ‘ReplaceDocumentAsync’ method creates a new document in Azure Cosmos DB.
- True
- False
Answer: False
Explanation: ‘ReplaceDocumentAsync’ method is used to replace or update a document in Azure Cosmos DB, not to create a new document.
In Azure Cosmos DB, which HTTP method is used with point operations for creating a new document?
- A. PATCH
- B. GET
- C. POST
- D. DELETE
Answer: C. POST
Explanation: In Azure Cosmos DB, the HTTP POST method is used with point operations to create a new document.
True or False: ‘Upsert’ operation in Azure Cosmos DB can be used for either creating a new document or updating an existing one.
- True
- False
Answer: True
Explanation: ‘Upsert’ operation in Azure Cosmos DB will create a new document if it doesn’t exist, or update it if it does.
Which of the following is the correct JSON structure for creating a document in Azure Cosmos DB?
- A. {“id”: “1”, “firstname”: “John”, “lastname”: “Doe”}
- B. <id>1</id><firstname>John</firstname><lastname>Doe</lastname>
- C. id:1, firstname:John, lastname:Doe
- D. “id”->”1″, “firstname”->”John”, “lastname”->”Doe”
Answer: A. {“id”: “1”, “firstname”: “John”, “lastname”: “Doe”}
Explanation: Azure Cosmos DB uses JSON for its data model. The correct JSON structure for creating a document would be {“id”: “1”, “firstname”: “John”, “lastname”: “Doe”}.
Interview Questions
What is Cosmos DB in Microsoft Azure?
Cosmos DB is a fully-managed NoSQL database service in Microsoft Azure that offers global distribution, automatic indexing and guarantees high performance and availability.
How can you create a document in Azure Cosmos DB?
We can create a document in Cosmos DB using the Azure portal or programmatically by using Azure Cosmos DB .NET SDK or any other supported SDK. In code, we may use the CreateDocumentAsync method.
How would you update a document in Azure Cosmos DB?
We can update a document in Cosmos DB using the ReplaceDocumentAsync method, that replaces an existing document with a new one.
How can we delete a document in Azure Cosmos DB?
We can delete a document by selecting it in the Azure portal and clicking “Delete”. Programmatically, we can use the DeleteDocumentAsync method with the document link as the parameter.
What is meant by a ‘point operation’ in Cosmos DB in context of documents?
A point operation refers to actions like document creation, retrieval, update or deletion in Cosmos DB that are performed against a single specific entity or key.
What role does the upsert operation play in Cosmos DB?
The upsert operation either creates a new document if it doesn’t exist or updates an existing document. It’s a combination of “update” and “insert”.
How does concurrency control work in Cosmos DB during document updates?
Cosmos DB provides optimistic concurrency control. Every document has a _etag property, which is updated every time a document is updated. When updating a document, you can include the etag value and if the current etag matches, Cosmos DB will proceed with the update.
Can you update multiple documents at once in Azure Cosmos DB?
Directly, No. Cosmos DB currently doesn’t support multiple document updates in a single transaction outside of stored procedures and triggers.
Why is it important to define a partition key while creating documents in Cosmos DB?
A partition key is responsible for distributing data across multiple logical partitions, ensuring that documents that are often queried together are stored in the same partition for optimal query and transaction efficiency.
What is the id property of a document in Cosmos DB?
“id” is a unique identifier that is given to each document in Cosmos DB. It’s a string value and is mandatory for every document.
What would happen if you attempted to delete a document using DeleteDocumentAsync method, but the document does not exist?
If attempted, the DeleteDocumentAsync method throws a DocumentClientException with StatusCode set as HttpStatusCode.NotFound.
What is optimistic concurrency in Cosmos DB?
Optimistic concurrency is when you read a document, make some changes, and write the document back, assuming that no other client has modified the document in the meantime. It’s validated with entity tags (_etag) within Cosmos DB.
What is the purpose of the UpsertDocumentAsync method in Cosmos DB?
The UpsertDocumentAsync method in Cosmos DB is used to create new documents if they don’t exist, or update the documents if they already exist in the database.
Can you perform operations on a group of documents in a single API call in Cosmos DB?
With transactions in stored procedures or triggers, yes. However, outside of these, Cosmos DB does not currently support batch operations.
What is the function of the ReplaceDocumentAsync method in Cosmos DB?
The ReplaceDocumentAsync method in Cosmos DB is used to completely replace an existing document with a new one, essentially updating it.