Skip to content

Commit 59cf086

Browse files
authored
Merge pull request #229578 from TimShererWithAquent/us2036619c
Freshness Pass User Story: 2036619 Get started with Azure Cosmos DB Partial Document Update
2 parents ab595d2 + a64b317 commit 59cf086

File tree

2 files changed

+75
-73
lines changed

2 files changed

+75
-73
lines changed

articles/cosmos-db/nosql/TOC.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@
476476
items:
477477
- name: Partial document update overview
478478
href: ../partial-document-update.md
479-
- name: Getting started with Partial document update
479+
- name: Get started with Partial Document Update
480480
href: ../partial-document-update-getting-started.md
481481
- name: Partial document update FAQ
482482
href: ../partial-document-update-faq.yml

articles/cosmos-db/partial-document-update-getting-started.md

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
---
2-
title: Getting started with Azure Cosmos DB Partial Document Update
3-
description: This article provides example for how to use Partial Document Update with .NET, Java, Node SDKs
2+
title: Get started with Azure Cosmos DB Partial Document Update
3+
description: Learn how to use Partial Document Update with .NET, Java, and Node SDKs for Azure Cosmos DB with these examples.
44
author: seesharprun
55
ms.service: cosmos-db
66
ms.subservice: nosql
77
ms.topic: how-to
8-
ms.date: 12/09/2021
8+
ms.date: 03/06/2023
99
ms.author: sidandrews
1010
ms.custom: ignite-fall-2021, ignite-2022
1111
---
1212

13-
# Azure Cosmos DB Partial Document Update: Getting Started
13+
# Get started with Azure Cosmos DB Partial Document Update
1414
[!INCLUDE[NoSQL](includes/appliesto-nosql.md)]
1515

16-
This article provides examples illustrating for how to use Partial Document Update with .NET, Java, and Node SDKs. This article also details common errors that you may encounter. Code samples for the following scenarios have been provided:
16+
This article provides examples that illustrate how to use Partial Document Update with .NET, Java, and Node SDKs. It also describes common errors that you might encounter.
1717

18-
- Executing a single patch operation
19-
- Combining multiple patch operations
20-
- Conditional patch syntax based on filter predicate
21-
- Executing patch operation as part of a Transaction
18+
This article links to code samples for the following scenarios:
19+
20+
- Run a single patch operation
21+
- Combine multiple patch operations
22+
- Use conditional patch syntax based on filter predicate
23+
- Run patch operation as part of a transaction
2224

2325
## [.NET](#tab/dotnet)
2426

25-
Support for Partial document update (Patch API) in the [Azure Cosmos DB .NET v3 SDK](nosql/sdk-dotnet-v3.md) is available from version *3.23.0* onwards. You can download it from the [NuGet Gallery](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.23.0)
27+
Support for Partial Document Update (Patch API) in the [Azure Cosmos DB .NET v3 SDK](nosql/sdk-dotnet-v3.md) is available starting with version *3.23.0*. You can download it from the [NuGet Gallery](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.23.0).
2628

2729
> [!NOTE]
28-
> A complete partial document update sample can be found in the [.NET v3 samples repository](https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs) on GitHub.
30+
> Find a complete Partial Document Update sample in the [.NET v3 samples repository](https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs) on GitHub.
2931
30-
- Executing a single patch operation
32+
- Run a single patch operation:
3133

3234
```csharp
3335
ItemResponse<Product> response = await container.PatchItemAsync<Product>(
@@ -41,7 +43,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB .NET v3
4143
Product updated = response.Resource;
4244
```
4345

44-
- Combining multiple patch operations
46+
- Combine multiple patch operations:
4547

4648
```csharp
4749
List<PatchOperation> operations = new ()
@@ -58,7 +60,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB .NET v3
5860
);
5961
```
6062

61-
- Conditional patch syntax based on filter predicate
63+
- Use conditional patch syntax based on filter predicate:
6264

6365
```csharp
6466
PatchItemRequestOptions options = new()
@@ -79,7 +81,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB .NET v3
7981
);
8082
```
8183

82-
- Executing patch operation as a part of a Transaction
84+
- Run patch operation as a part of a transaction:
8385

8486
```csharp
8587
TransactionalBatchPatchItemRequestOptions options = new()
@@ -113,7 +115,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB .NET v3
113115

114116
## [Java](#tab/java)
115117

116-
Support for Partial document update (Patch API) in the [Azure Cosmos DB Java v4 SDK](nosql/sdk-java-v4.md) is available from version *4.21.0* onwards. You can either add it to the list of dependencies in your `pom.xml` or download it directly from [Maven](https://mvnrepository.com/artifact/com.azure/azure-cosmos).
118+
Support for Partial Document Update (Patch API) in the [Azure Cosmos DB Java v4 SDK](nosql/sdk-java-v4.md) is available starting with version *4.21.0*. You can either add it to the list of dependencies in your `pom.xml` or download it directly from [Maven](https://mvnrepository.com/artifact/com.azure/azure-cosmos).
117119
118120
```xml
119121
<dependency>
@@ -124,9 +126,9 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB Java v4
124126
```
125127

126128
> [!NOTE]
127-
> The full sample can be found in the [Java SDK v4 samples repository](https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/tree/main/src/main/java/com/azure/cosmos/examples/patch/sync) on GitHub
129+
> Find the full sample in the [Java SDK v4 samples repository](https://github.com/Azure-Samples/azure-cosmos-java-sql-api-samples/tree/main/src/main/java/com/azure/cosmos/examples/patch/sync) on GitHub.
128130
129-
- Executing a single patch operation
131+
- Run a single patch operation:
130132

131133
```java
132134
CosmosItemResponse<Product> response = container.patchItem(
@@ -141,7 +143,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB Java v4
141143
Product updated = response.getItem();
142144
```
143145

144-
- Combining multiple patch operations
146+
- Combine multiple patch operations:
145147

146148
```java
147149
CosmosPatchOperations operations = CosmosPatchOperations
@@ -158,7 +160,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB Java v4
158160
);
159161
```
160162

161-
- Conditional patch syntax based on filter predicate
163+
- Use conditional patch syntax based on filter predicate:
162164

163165
```java
164166
CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
@@ -177,7 +179,7 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB Java v4
177179
);
178180
```
179181

180-
- Executing patch operation as a part of a Transaction
182+
- Run patch operation as a part of a transaction:
181183

182184
```java
183185
CosmosBatchPatchItemRequestOptions options = new CosmosBatchPatchItemRequestOptions();
@@ -208,14 +210,12 @@ Support for Partial document update (Patch API) in the [Azure Cosmos DB Java v4
208210

209211
## [Node.js](#tab/nodejs)
210212

211-
Support for Partial document update (Patch API) in the [Azure Cosmos DB JavaScript SDK](nosql/sdk-nodejs.md) is available from version *3.15.0* onwards. You can download it from the [npm Registry](https://www.npmjs.com/package/@azure/cosmos)
213+
Support for Partial Document Update (Patch API) in the [Azure Cosmos DB JavaScript SDK](nosql/sdk-nodejs.md) is available starting with version *3.15.0*. You can download it from the [npm Registry](https://www.npmjs.com/package/@azure/cosmos).
212214
213215
> [!NOTE]
214-
> A complete partial document update sample can be found in the [.js v3 samples repository](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ItemManagement.ts#L167) on GitHub. In the sample, as the container is created without a partition key specified, the JavaScript SDK
215-
resolves the partition key values from the items through the container's partition
216-
key definition.
216+
> Find a complete Partial Document Update sample in the [.js v3 samples repository](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ItemManagement.ts#L167) on GitHub. In the sample, as the container is created without a partition key specified, the JavaScript SDK resolves the partition key values from the items through the container's partition key definition.
217217
218-
- Executing a single patch operation
218+
- Run a single patch operation:
219219

220220
```javascript
221221
const operations =
@@ -231,7 +231,7 @@ key definition.
231231
.patch(operations);
232232
```
233233

234-
- Combining multiple patch operations
234+
- Combine multiple patch operations:
235235

236236
```javascript
237237
const operations =
@@ -248,7 +248,7 @@ key definition.
248248
.patch(operations);
249249
```
250250

251-
- Conditional patch syntax based on filter predicate
251+
- Use conditional patch syntax based on filter predicate:
252252

253253
```javascript
254254
const filter = 'FROM products p WHERE p.used = false'
@@ -271,9 +271,9 @@ key definition.
271271

272272
---
273273

274-
## Support for Server-Side programming
274+
## Support for server-side programming
275275

276-
Partial Document Update operations can also be [executed on the server-side](stored-procedures-triggers-udfs.md) using Stored procedures, triggers, and user-defined functions.
276+
Partial Document Update operations can also be [executed on the server-side](stored-procedures-triggers-udfs.md) using stored procedures, triggers, and user-defined functions.
277277

278278
```javascript
279279
this.patchDocument = function (documentLink, patchSpec, options, callback) {
@@ -321,57 +321,59 @@ this.patchDocument = function (documentLink, patchSpec, options, callback) {
321321
);
322322
};
323323
```
324+
324325
> [!NOTE]
325-
> Definition of validateOptionsAndCallback can be found in the [.js DocDbWrapperScript](https://github.com/Azure/azure-cosmosdb-js-server/blob/1dbe69893d09a5da29328c14ec087ef168038009/utils/DocDbWrapperScript.js#L289) on GitHub.
326+
> Find the definition of `validateOptionsAndCallback` in the [.js DocDbWrapperScript](https://github.com/Azure/azure-cosmosdb-js-server/blob/1dbe69893d09a5da29328c14ec087ef168038009/utils/DocDbWrapperScript.js#L289) on GitHub.
326327
327-
- Sample parameter for patch operation
328+
Sample parameter for patch operation:
328329

329-
```javascript
330-
function () {
331-
var doc = {
332-
"id": "exampleDoc",
333-
"field1": {
334-
"field2": 10,
335-
"field3": 20
336-
}
337-
};
338-
var isAccepted = __.createDocument(__.getSelfLink(), doc, (err, doc) => {
339-
if (err) throw err;
340-
var patchSpec = [
341-
{"op": "add", "path": "/field1/field2", "value": 20},
342-
{"op": "remove", "path": "/field1/field3"}
343-
];
344-
isAccepted = __.patchDocument(doc._self, patchSpec, (err, doc) => {
345-
if (err) throw err;
346-
else {
347-
getContext().getResponse().setBody(docPatched);
348-
}
349-
}
350-
}
351-
if(!isAccepted) throw new Error("patch was't accepted")
352-
}
353-
}
354-
if(!isAccepted) throw new Error("create wasn't accepted")
355-
}
356-
```
330+
```javascript
331+
function () {
332+
var doc = {
333+
"id": "exampleDoc",
334+
"field1": {
335+
"field2": 10,
336+
"field3": 20
337+
}
338+
};
339+
var isAccepted = __.createDocument(__.getSelfLink(), doc, (err, doc) => {
340+
if (err) throw err;
341+
var patchSpec = [
342+
{"op": "add", "path": "/field1/field2", "value": 20},
343+
{"op": "remove", "path": "/field1/field3"}
344+
];
345+
isAccepted = __.patchDocument(doc._self, patchSpec, (err, doc) => {
346+
if (err) throw err;
347+
else {
348+
getContext().getResponse().setBody(docPatched);
349+
}
350+
}
351+
}
352+
if(!isAccepted) throw new Error("patch was't accepted")
353+
}
354+
}
355+
if(!isAccepted) throw new Error("create wasn't accepted")
356+
}
357+
```
357358

358359
## Troubleshooting
359360

360-
Here's a list of common errors that you might encounter while using this feature:
361+
Here's some common errors that you might encounter while using this feature:
361362

362363
| **Error Message** | **Description** |
363364
| ------------ | -------- |
364-
| Invalid patch request: check syntax of patch specification| The Patch operation syntax is invalid. For more information, see [the partial document update specification](partial-document-update.md#rest-api-reference-for-partial-document-update)
365-
| Invalid patch request: Can't patch system property `SYSTEM_PROPERTY`. | System-generated properties like `_id`, `_ts`, `_etag`, `_rid` aren't modifiable using a Patch operation. For more information, see: [Partial Document Update FAQs](partial-document-update-faq.yml#is-partial-document-update-supported-for-system-generated-properties-)
366-
| The number of patch operations can't exceed 10 | There's a limit of 10 patch operations that can be added in a single patch specification. For more information, see: [Partial Document Update FAQs](partial-document-update-faq.yml#is-there-a-limit-to-the-number-of-partial-document-update-operations-)
367-
| For Operation(`PATCH_OPERATION_INDEX`): Index(`ARRAY_INDEX`) to operate on is out of array bounds | The index of array element to be patched is out of bounds
368-
| For Operation(`PATCH_OPERATION_INDEX`)): Node(`PATH`) to be replaced has been removed earlier in the transaction.| The path you're trying to patch doesn't exist.
369-
| For Operation(`PATCH_OPERATION_INDEX`): Node(`PATH`) to be removed is absent. Note: it may also have been removed earlier in the transaction.  | The path you're trying to patch doesn't exist.
370-
| For Operation(`PATCH_OPERATION_INDEX`): Node(`PATH`) to be replaced is absent. | The path you're trying to patch doesn't exist.
371-
| For Operation(`PATCH_OPERATION_INDEX`): Node(`PATH`) isn't a number.| Increment operation can only work on integer and float. For more information, see: [Supported Operations](partial-document-update.md#supported-operations)
372-
| For Operation(`PATCH_OPERATION_INDEX`): Add Operation can only create a child object of an existing node(array or object) and can't create path recursively, no path found beyond: `PATH`. | Child paths can be added to an object or array node type. Also, to create `n`th child, `n-1`th child should be present
373-
| For Operation(`PATCH_OPERATION_INDEX`): Given Operation can only create a child object of an existing node(array or object) and can't create path recursively, no path found beyond: `PATH`. | Child paths can be added to an object or array node type. Also, to create `n`th child, `n-1`th child should be present
365+
| Invalid patch request: check syntax of patch specification. | The patch operation syntax is invalid. For more information, see [the Partial Document Update specification](partial-document-update.md#rest-api-reference-for-partial-document-update). |
366+
| Invalid patch request: Can't patch system property `SYSTEM_PROPERTY`. | System-generated properties like `_id`, `_ts`, `_etag`, `_rid` aren't modifiable using a patch operation. For more information, see [Partial Document Update FAQs](partial-document-update-faq.yml#is-partial-document-update-supported-for-system-generated-properties-). |
367+
| The number of patch operations can't exceed 10. | There's a limit of 10 patch operations that can be added in a single patch specification. For more information, see [Partial Document Update FAQs](partial-document-update-faq.yml#is-there-a-limit-to-the-number-of-partial-document-update-operations-). |
368+
| For Operation(`PATCH_OPERATION_INDEX`): Index(`ARRAY_INDEX`) to operate on is out of array bounds. | The index of array element to be patched is out of bounds. |
369+
| For Operation(`PATCH_OPERATION_INDEX`)): Node(`PATH`) to be replaced has been removed earlier in the transaction. | The path you're trying to patch doesn't exist. |
370+
| For Operation(`PATCH_OPERATION_INDEX`): Node(`PATH`) to be removed is absent. Note: it might also have been removed earlier in the transaction. | The path you're trying to patch doesn't exist. |
371+
| For Operation(`PATCH_OPERATION_INDEX`): Node(`PATH`) to be replaced is absent. | The path you're trying to patch doesn't exist. |
372+
| For Operation(`PATCH_OPERATION_INDEX`): Node(`PATH`) isn't a number. | Increment operation can only work on integer and float. For more information, see: [Supported Operations](partial-document-update.md#supported-operations). |
373+
| For Operation(`PATCH_OPERATION_INDEX`): Add Operation can only create a child object of an existing node (array or object) and can't create path recursively, no path found beyond: `PATH`. | Child paths can be added to an object or array node type. Also, to create `n`th child, `n-1`th child should be present. |
374+
| For Operation(`PATCH_OPERATION_INDEX`): Given Operation can only create a child object of an existing node(array or object) and can't create path recursively, no path found beyond: `PATH`. | Child paths can be added to an object or array node type. Also, to create `n`th child, `n-1`th child should be present. |
374375

375376
## Next steps
376377

377-
- Review the conceptual overview of [Partial Document Update](partial-document-update.md)
378+
- [Partial Document Update in Azure Cosmos DB](partial-document-update.md)
379+
- [Frequently asked questions about Partial Document Update in Azure Cosmos DB](partial-document-update-faq.yml)

0 commit comments

Comments
 (0)