Skip to content

Commit a3a4dca

Browse files
authored
Merge pull request #109180 from jcbartle/jcbartle-patch-1
Updated the JavaScript / stored procedure sample code
2 parents f41a7b5 + 1305950 commit a3a4dca

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -335,34 +335,46 @@ this.patchDocument = function (documentLink, patchSpec, options, callback) {
335335
> [!NOTE]
336336
> 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.
337337
338-
Sample parameter for patch operation:
338+
Sample stored procedure for patch operation:
339339

340340
```javascript
341-
function () {
341+
function patchDemo() {
342342
var doc = {
343-
"id": "exampleDoc",
344-
"field1": {
345-
"field2": 10,
346-
"field3": 20
347-
}
348-
};
349-
var isAccepted = __.createDocument(__.getSelfLink(), doc, (err, doc) => {
350-
if (err) throw err;
351-
var patchSpec = [
352-
{"op": "add", "path": "/field1/field2", "value": 20},
353-
{"op": "remove", "path": "/field1/field3"}
354-
];
355-
isAccepted = __.patchDocument(doc._self, patchSpec, (err, doc) => {
356-
if (err) throw err;
357-
else {
358-
getContext().getResponse().setBody(docPatched);
359-
}
360-
}
361-
}
362-
if(!isAccepted) throw new Error("patch was't accepted")
363-
}
364-
}
365-
if(!isAccepted) throw new Error("create wasn't accepted")
343+
"id": "exampleDoc",
344+
"fields": {
345+
"field1": "exampleString",
346+
"field2": 20,
347+
"field3": 40
348+
}
349+
};
350+
351+
var isAccepted = __.createDocument(__.getSelfLink(), doc, (err, doc) => {
352+
if (err) {
353+
throw err;
354+
}
355+
else {
356+
getContext().getResponse().setBody("Example document successfully created.");
357+
358+
var patchSpec = [
359+
{ "op": "add", "path": "/fields/field1", "value": "newExampleString" },
360+
{ "op": "remove", "path": "/fields/field2" },
361+
{ "op": "incr", "path": "/fields/field3", "value": 10 }
362+
];
363+
364+
var isAccepted = __.patchDocument(doc._self, patchSpec, (err, doc) => {
365+
if (err) {
366+
throw err;
367+
}
368+
else {
369+
getContext().getResponse().appendBody(" Example document successfully patched.");
370+
}
371+
});
372+
373+
if (!isAccepted) throw new Error("Patch wasn't accepted");
374+
}
375+
});
376+
377+
if (!isAccepted) throw new Error("Create wasn't accepted.");
366378
}
367379
```
368380

0 commit comments

Comments
 (0)