{"id":3587,"date":"2021-11-02T08:00:05","date_gmt":"2021-11-02T15:00:05","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=3587"},"modified":"2023-05-05T07:13:22","modified_gmt":"2023-05-05T14:13:22","slug":"partial-document-update-ga","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/partial-document-update-ga\/","title":{"rendered":"Now Generally Available \u2013 Partial Document Update in Azure Cosmos DB"},"content":{"rendered":"<p style=\"text-align: center;\">By Saranya Sriram, Abhishek Gupta<\/p>\n<hr \/>\n<p><span class=\"TextRun BCX0 SCXW21573702\" lang=\"EN-IN\" xml:lang=\"EN-IN\" data-contrast=\"none\"><span class=\"NormalTextRun BCX0 SCXW21573702\">We&#8217;re excited to announce the general availability of <\/span><\/span><a class=\"Hyperlink BCX0 SCXW21573702\" href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/partial-document-update\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined BCX0 SCXW21573702\" lang=\"EN-IN\" xml:lang=\"EN-IN\" data-contrast=\"none\"><span class=\"NormalTextRun BCX0 SCXW21573702\" data-ccp-charstyle=\"Hyperlink\">partial document update for the Azure Cosmos DB <\/span><span class=\"NormalTextRun BCX0 SCXW21573702\" data-ccp-charstyle=\"Hyperlink\">Core (<\/span><span class=\"NormalTextRun BCX0 SCXW21573702\" data-ccp-charstyle=\"Hyperlink\">SQL<\/span><span class=\"NormalTextRun BCX0 SCXW21573702\" data-ccp-charstyle=\"Hyperlink\">)<\/span><span class=\"NormalTextRun BCX0 SCXW21573702\" data-ccp-charstyle=\"Hyperlink\">\u00a0API<\/span><\/span><\/a>, which was announced at Microsoft Ignite<span class=\"TextRun BCX0 SCXW21573702\" lang=\"EN-IN\" xml:lang=\"EN-IN\" data-contrast=\"none\"><span class=\"NormalTextRun BCX0 SCXW21573702\">!<\/span><span class=\"NormalTextRun BCX0 SCXW21573702\">\u00a0<\/span><span class=\"NormalTextRun BCX0 SCXW21573702\">This new feature makes it possible to<\/span><span class=\"NormalTextRun BCX0 SCXW21573702\"> perform path-level updates to specific fields\/properties in a single document without needing to perform a full document read-replace operation.\u00a0<\/span><\/span>Partial document update is currently supported in Azure Cosmos DB <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/sql-api-sdk-dotnet-standard\">.NET SDK<\/a>,\u00a0<a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/sql-api-sdk-java-v4\">Java SDK<\/a>,\u00a0<a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/sql-api-sdk-node\">Node SDK<\/a>,\u00a0and\u00a0<a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/how-to-write-stored-procedures-triggers-udfs?tabs=javascript\">stored procedures<\/a>.<\/p>\n<h3>Document updates &#8211; then and now<\/h3>\n<p><span data-contrast=\"none\">To put things in context, here is a refresher on how one would typically use a document replace operation:\u00a0<\/span><span data-contrast=\"none\">Read the document, u<\/span><span data-contrast=\"none\">pdate it locally (client side) including any <\/span><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql\/database-transactions-optimistic-concurrency#optimistic-concurrency-control\"><span data-contrast=\"none\">optimistic \u00a0<\/span><\/a><span data-contrast=\"none\">concurrency\u00a0control<\/span><span data-contrast=\"none\"> (OCC) checks if necessary and, finally call the<\/span><span data-contrast=\"none\">\u00a0replace\u00a0operation along\u00a0with the updated document.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">Here is a trimmed down example of how one would use partial document update (<\/span><span data-contrast=\"auto\">using\u00a0the\u00a0Java SDK<\/span><span data-contrast=\"none\">):<\/span><span data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">\/\/step 1\u00a0\r\n\r\nUserInfo user = container.readItem(user.getId(), new PartitionKey(user.getEmail()), UserInfo.class);\u00a0\r\n\r\n\u00a0\r\n\/\/step 2\u00a0\r\n\r\nCosmosPatchOperations patchOps = CosmosPatchOperations.create().add(\"\/phone\/2\",12345).set(\"\/address\",\"123 Foobar\");\u00a0\r\n\r\n\u00a0\r\n\/\/step 3\u00a0\r\n\r\ncontainer.patchItem(user.getId(), new PartitionKey(user.getEmail()), patchOps, reqOps, UserInfo.class);<\/pre>\n<ol>\n<li data-leveltext=\"%1.\" data-font=\"Segoe UI,Times New Roman\" data-listid=\"9\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Read the document (this is the same as\u00a0replace) with\u00a0<\/span><a href=\"https:\/\/azuresdkdocs.blob.core.windows.net\/$web\/java\/azure-cosmos\/latest\/com\/azure\/cosmos\/CosmosContainer.html#readItem-java.lang.String-com.azure.cosmos.models.PartitionKey-java.lang.Class-\"><span data-contrast=\"none\">readItem<\/span><\/a><span data-contrast=\"none\">\u00a0<\/span><span data-contrast=\"none\">method.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Segoe UI,Times New Roman\" data-listid=\"9\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">Define the updates you want to make (in form of a\u00a0<\/span><a href=\"https:\/\/azuresdkdocs.blob.core.windows.net\/$web\/java\/azure-cosmos\/latest\/com\/azure\/cosmos\/models\/CosmosPatchOperations.html\"><span data-contrast=\"none\">CosmosPatchOperations<\/span><\/a><span data-contrast=\"none\">\u00a0object\u00a0\u2013 in this case we add\u00a0a\u00a0<\/span><i><span data-contrast=\"none\">phone<\/span><\/i><span data-contrast=\"none\">\u00a0number (an array) and set the\u00a0<\/span><i><span data-contrast=\"none\">address<\/span><\/i><span data-contrast=\"none\">\u00a0to\u00a0a\u00a0different one.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"%1.\" data-font=\"Segoe UI,Times New Roman\" data-listid=\"9\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span data-contrast=\"none\">Invoke the\u202f<\/span><a href=\"https:\/\/azuresdkdocs.blob.core.windows.net\/$web\/java\/azure-cosmos\/latest\/com\/azure\/cosmos\/CosmosContainer.html#patchItem-java.lang.String-com.azure.cosmos.models.PartitionKey-com.azure.cosmos.models.CosmosPatchOperations-com.azure.cosmos.models.CosmosPatchItemRequestOptions-java.lang.Class-\"><span data-contrast=\"none\">patchItem<\/span><span data-contrast=\"none\">\u202f<\/span><\/a><span data-contrast=\"none\">method.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<\/ol>\n<h3><b><span data-contrast=\"none\">Benefits of partial document update<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:180}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">When compared with the replace operation, the overhead of serializing the entire document is eliminated since the client application <\/span><i><span data-contrast=\"none\">only\u00a0<\/span><\/i><span data-contrast=\"none\">needs to deal with properties to be updated (<\/span><i><span data-contrast=\"none\">phone<\/span><\/i><span data-contrast=\"none\">\u00a0and\u00a0<\/span><i><span data-contrast=\"none\">address<\/span><\/i><span data-contrast=\"none\"> in this example). This becomes significant when your application makes frequent updates to only a few properties in your documents, such as incrementing counters, toggling true\/false flags or similar types of changes.<\/span><\/p>\n<p><span data-contrast=\"none\">Not having to send the entire document over the wire impacts overall application performance &#8211; reduced network bandwidth usage, lower end-to end-latency and savings of CPU cycles on the Azure Cosmos DB SDK client hosts.<\/span><span data-ccp-props=\"{&quot;335559738&quot;:180}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">Some of the other developer productivity benefits that are a by-product of the flexible\u00a0programming model enabled by partial document update:<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;335559738&quot;:180}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Combining multiple operations<\/span><\/b><span data-contrast=\"none\">:\u00a0Partial\u00a0document update supports\u00a0different types of operations\u00a0(see next section). Depending on your requirements, you\u00a0can\u00a0combine multiple\u00a0such\u00a0operations\u00a0as part of a single invocation as opposed to incurring cost of\u00a0round trips for separate operation type.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Conditional update<\/span><\/b><span data-contrast=\"none\">: If you want the partial update to depend on a pre-condition, you can\u00a0define it\u00a0using\u00a0a\u00a0SQL-like filter predicate (for example,\u00a0<\/span><i><span data-contrast=\"none\">from c where\u00a0c.taskNum\u00a0= 3<\/span><\/i><span data-contrast=\"none\">).\u00a0The partial update\u00a0operation\u00a0will\u00a0fail if the pre-condition specified in the predicate is not satisfied.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Transactions support<\/span><\/b><span data-contrast=\"none\">:\u00a0Partial document update works in the context of a\u00a0<\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/sql\/transactional-batch\"><span data-contrast=\"none\">Transactional batch<\/span><\/a><span data-contrast=\"none\"> as well.\u00a0This means that multiple documents within the same partition key can be patched\u00a0(partially updated)\u00a0as part of a transaction\u00a0(along with other operations such as create). The transaction\u00a0will be committed only if all the operations succeed. If any operation fails, the entire transaction is rolled back.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"11\" aria-setsize=\"-1\" data-aria-posinset=\"4\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Transparent\u00a0conflict resolution<\/span><\/b><span data-contrast=\"none\">:\u00a0If your Azure Cosmos DB account is configured with multiple write regions,\u00a0<\/span><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/conflict-resolution-policies\"><span data-contrast=\"none\">conflicts and\u00a0respective\u00a0resolution policies<\/span><\/a><span data-contrast=\"none\"> are applicable at the document level (with Last Write Wins being the default conflict resolution policy). This works differently in case of partial document updates &#8211; conflicts that occur due to concurrent patch operations to the same document across multiple regions will be detected and resolved at the <\/span><i><span data-contrast=\"none\">path-level<\/span><\/i><span data-contrast=\"none\">. This\u00a0means that as long as you\u2019re updating\u00a0<\/span><i><span data-contrast=\"none\">different\u00a0<\/span><\/i><span data-contrast=\"none\">properties\u00a0(paths)\u00a0in the\u00a0<\/span><i><span data-contrast=\"none\">same\u00a0<\/span><\/i><span data-contrast=\"none\">document, they will be merged successfully.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<\/ul>\n<h3><b><span data-contrast=\"none\">Partial\u00a0document update operations<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:315}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">Although Partial document update is a top-level operation (just like Replace), it supports sub-operations (the code snippet above used <\/span><strong>add<\/strong> <span data-contrast=\"none\">and <\/span><strong>set<\/strong>)<span data-contrast=\"none\">. You can\u00a0<\/span><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/partial-document-update#supported-operations\"><span data-contrast=\"none\">refer to the documentation<\/span><\/a><span data-contrast=\"none\"> for details, but here is a summary:<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"10\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Add<\/span><\/b><span data-contrast=\"none\">: Creates a new element (if it does not already exist).<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"10\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Set<\/span><\/b><span data-contrast=\"none\">: Updates an element (creates one if it does not already exist).<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"10\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Replace<\/span><\/b><span data-contrast=\"none\">: Updates an element only if it already exists.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"10\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Remove<\/span><\/b><span data-contrast=\"none\">: Deletes an existing element.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"10\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Increment<\/span><\/b><span data-contrast=\"none\">: Increases\/decreases by specified value (use negative value to decrease).<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<\/ul>\n<h3><b><span data-contrast=\"none\">Learn more<\/span><\/b><span data-ccp-props=\"{&quot;335559738&quot;:315}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">Find out more about Azure Cosmos DB partial document update:<\/span><span data-ccp-props=\"{&quot;335551550&quot;:1,&quot;335551620&quot;:1,&quot;335559738&quot;:315,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"12\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/partial-document-update\"><span data-contrast=\"none\">Read the concepts<\/span><\/a><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"12\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/partial-document-update-getting-started\"><span data-contrast=\"none\">Code samples<\/span><\/a><span data-contrast=\"none\">\u00a0to get you started<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"12\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Add\/Set\/Replace\u00a0operations \u2013\u00a0<\/span><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/partial-document-update#similarities-and-differences\"><span data-contrast=\"none\">how are they\u00a0similar,\u00a0yet different<\/span><\/a><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"12\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/partial-document-update-faq\"><span data-contrast=\"none\">Frequently asked questions<\/span><\/a><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/azure.microsoft.com\/services\/cosmos-db\/#overview\" target=\"_blank\" rel=\"noopener\">Get started free<\/a> with Azure Cosmos DB.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check out the new partial document update feature for Azure Cosmos DB Core (SQL) API, announced at Microsoft Ignite.  This feature makes it possible to use the Java, .NET, and Node.js SDKs to perform path-level updates to specific fields\/properties in a single document without needing to perform a full document read-replace operation.<\/p>\n","protected":false},"author":38710,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[12,14],"tags":[1864,957,1865,1863],"class_list":["post-3587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements","category-core-sql-api","tag-azurecosmosdb","tag-cosmosdb","tag-databases","tag-patch"],"acf":[],"blog_post_summary":"<p>Check out the new partial document update feature for Azure Cosmos DB Core (SQL) API, announced at Microsoft Ignite.  This feature makes it possible to use the Java, .NET, and Node.js SDKs to perform path-level updates to specific fields\/properties in a single document without needing to perform a full document read-replace operation.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/3587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/38710"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=3587"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/3587\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/61"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=3587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=3587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=3587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}