{"id":10199,"date":"2023-06-23T01:43:43","date_gmt":"2023-06-23T08:43:43","guid":{"rendered":"https:\/\/www.couchbase.com\/developers\/tutorials\/document-revisions\/"},"modified":"2023-06-23T01:43:43","modified_gmt":"2023-06-23T08:43:43","slug":"document-revisions","status":"publish","type":"tutorials","link":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/","title":{"rendered":"Document Revisions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">From time to time, there may be a requirement to save multiple revisions of a document within a bucket, this history is maintained by the application. This section is intended to outline one way this can be accomplished.<\/p>\n\n\n\n<h4 id=\"h-document-metadata\" class=\"wp-block-heading\">Document Metadata<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Just as we&#8217;ve shown in previous sections, we&#8217;ll add a standardized metadata property to our document model to assist with storing the revision information. A certain amount of this is standard practice around the industry. Building on our User model, we can add a <code>_ver<\/code> property to document the current version of the document.<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{  \n  &quot;_type&quot;:&quot;user&quot;,  \n  &quot;_schema&quot;: &quot;3.0&quot;,\n  &quot;_ver&quot;: 1,\n  &quot;_created&quot;: 1544759124,\n  &quot;userId&quot;: 123,  \n  &quot;firstName&quot;: &quot;Joe&quot;,\n  &quot;lastName&quot;: &quot;Smith&quot;,\n  &quot;phones&quot;: [\n    {\n      &quot;type&quot;: &quot;mobile&quot;,\n      &quot;number&quot;: &quot;1234567890&quot;\n    }\n  ]\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, in your data object, you would want to add some code to initialize and increment this revision number as updates are made to a document.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>public class User extends Base {\n  private var _ver: integer\n  private var userId: integer\n  private var firstName: string\n  private var lastName: string\n  private var phones: array\n  private var email: string\n\n  constructor User(doc) {\n    doc = migrate(doc) \/\/ transform the document\n    super(doc)\n    this.set_Type(&quot;user&quot;)\n    this.set_Schema(&quot;3.0&quot;)\n    \/\/ set _ver if it is not defined\n    if (!this._ver) {\n      this._ver = 1\n    }\n  }\n  ...\n}\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-maintaining-revisions\" class=\"wp-block-heading\">Maintaining Revisions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Because you are wanting to maintain the revisions of a document, this requires steps to be taken with writing updates to a document to preserve the previous version. It also requires using a predictable pattern in the revision key generation to make it easy to find and retrieve specific document revisions. One of the simplest ways of using a predictable pattern in the key generation is to append the revision number to the document key, so if the document had a key of <code>user:123<\/code>, the revision might have a key like <code>user:123:v:1<\/code>. So, if a revision had been made to our example user document, you&#8217;d have two different documents:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Document ID:<\/strong> <code>user:123<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{  \n  &quot;_type&quot;:&quot;user&quot;,  \n  &quot;_schema&quot;: &quot;3.0&quot;,\n  &quot;_ver&quot;: 2,\n  &quot;_created&quot;: 1544759124,\n  &quot;userId&quot;: 123,  \n  &quot;firstName&quot;: &quot;Joe&quot;,\n  &quot;lastName&quot;: &quot;Smith&quot;,\n  &quot;phones&quot;: [\n    {\n      &quot;type&quot;: &quot;mobile&quot;,\n      &quot;number&quot;: &quot;1234567890&quot;\n    },\n    {\n      &quot;type&quot;: &quot;home&quot;,\n      &quot;number&quot;: &quot;1234445555&quot;\n    }\n  ]\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Document ID:<\/strong> <code>user:123:v:1<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{  \n  &quot;_type&quot;:&quot;user&quot;,  \n  &quot;_schema&quot;: &quot;3.0&quot;,\n  &quot;_ver&quot;: 1,\n  &quot;_created&quot;: 1544759124,\n  &quot;userId&quot;: 123,  \n  &quot;firstName&quot;: &quot;Joe&quot;,\n  &quot;lastName&quot;: &quot;Smith&quot;,\n  &quot;phones&quot;: [\n    {\n      &quot;type&quot;: &quot;mobile&quot;,\n      &quot;number&quot;: &quot;1234567890&quot;\n    }\n  ]\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will require code in your objects <code>save()<\/code> method to make a copy of the current document prior to saving the update:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>public save() {\n  this.copyRevision()\n  this._ver++\n  bucket.upsert(\n    this._type + &quot;:&quot; + this.userId, \/\/ key\n    this \/\/ value\n  )\n}\n\nprivate copyRevision() {\n  var doc = bucket.get(this._type + &quot;:&quot; + this.userId)\n  bucket.insert(\n    this._type + this.userId + &quot;:v:&quot; + this._ver, \/\/ key\n    doc \/\/ value\n  )\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-limiting-revisions\" class=\"wp-block-heading\">Limiting Revisions<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The problem with maintaining document revisions is that they can significantly increase the amount of storage space needed to hold them. It&#8217;s not unusual for a document to be updated thousands of times over its lifespan. So odds are that you&#8217;ll want to limit the number of revisions to be kept. To avoid having to go back and purge outdated revisions, the better solution would be to determine ahead of time what your revision limit is going to be, and to build that into your data objects:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>private maxRevisionCount = 10\n\npublic class User extends Base {\n  ...\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then you would want to perform a check before saving a revision to see if an older revision needs to be deleted:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>public save() {\n  if (this._ver &gt;= maxRevisionCount) {\n    this.deleteOldRevision()\n  }\n  this.copyRevision()\n  this._ver++\n  bucket.upsert(\n    this._type + &quot;:&quot; + this.userId, \/\/ key\n    this \/\/ value\n  )\n}\n\nprivate deleteOldRevision() {\n  bucket.delete(this._type + &quot;:&quot; + this.userId + &quot;:v:&quot; + (this._ver - maxRevisionCount))\n}\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-summary\" class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By adding a revision number and using a key generation scheme that appends the revision number to the current revision key in a predictable way, it can be fairly straightforward to implement a document revision retention policy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In summary, you&#8217;ll need to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a revision number property to the document and data object.<\/li>\n\n\n<li>Increment the revision number property each time the document is updated.<\/li>\n\n\n<li>Copy the prior revision document, adding the revision number to the document key in a predictable way prior to saving any updates to the current revision of the document.<\/li>\n\n\n<li>Hard-code a maximum number of revisions to be kept.<\/li>\n\n\n<li>If the revision number of the document is greater than the maximum number of revisions being kept, subtract the maximum number of revisions from the current revision number and delete that version of the document when saving any updates to the current revision.<\/li>\n\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>From time to time, there may be a requirement to save multiple revisions of a document within a bucket, this history is maintained by the application. This section is intended to outline one way this can be accomplished. Document Metadata Just as we&#8217;ve shown in previous sections, we&#8217;ll add a standardized metadata property to our [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_acf":"","footnotes":""},"tutorial-languages":[551],"tutorial-tags":[565,567],"tutorial-technologies":[562,548,554],"tutorial-types":[555],"class_list":["post-10199","tutorials","type-tutorials","status-publish","hentry","tutorial-languages-any","tutorial-tags-data-modeling","tutorial-tags-metadata","tutorial-technologies-capella","tutorial-technologies-kv","tutorial-technologies-server","tutorial-types-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Document Revisions - Couchbase<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Document Revisions\" \/>\n<meta property=\"og:description\" content=\"From time to time, there may be a requirement to save multiple revisions of a document within a bucket, this history is maintained by the application. This section is intended to outline one way this can be accomplished. Document Metadata Just as we&#8217;ve shown in previous sections, we&#8217;ll add a standardized metadata property to our [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/\" \/>\n<meta property=\"og:site_name\" content=\"Couchbase\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Couchbase\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2548\" \/>\n\t<meta property=\"og:image:height\" content=\"1171\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@couchbase\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/developers\\\/tutorials\\\/document-revisions\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/developers\\\/tutorials\\\/document-revisions\\\/\",\"name\":\"Document Revisions - Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/#website\"},\"datePublished\":\"2023-06-23T08:43:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/developers\\\/tutorials\\\/document-revisions\\\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/developers\\\/tutorials\\\/document-revisions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/developers\\\/tutorials\\\/document-revisions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/ko\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Document Revisions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/\",\"name\":\"Couchbase\",\"description\":\"The Operational Data Platform for AI\u2122\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/#organization\"},\"alternateName\":\"Couchbase, Inc.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/#organization\",\"name\":\"Couchbase\",\"alternateName\":\"Couchbase, Inc.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"width\":1129,\"height\":1129,\"caption\":\"Couchbase\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Couchbase\\\/\",\"https:\\\/\\\/x.com\\\/couchbase\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/CouchbaseServer\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/couchbase\\\/\",\"https:\\\/\\\/github.com\\\/couchbase\\\/\",\"https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Couchbase,_Inc.\"],\"description\":\"Couchbase is a database software company headquartered in San Jose, California, providing an operational data platform for AI used by hundreds of global enterprises.\",\"legalName\":\"Couchbase, Inc.\",\"foundingDate\":\"2011-01-01\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Document Revisions - Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/","og_locale":"fr_FR","og_type":"article","og_title":"Document Revisions","og_description":"From time to time, there may be a requirement to save multiple revisions of a document within a bucket, this history is maintained by the application. This section is intended to outline one way this can be accomplished. Document Metadata Just as we&#8217;ve shown in previous sections, we&#8217;ll add a standardized metadata property to our [&hellip;]","og_url":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/","og_site_name":"Couchbase","article_publisher":"https:\/\/www.facebook.com\/Couchbase\/","og_image":[{"width":2548,"height":1171,"url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@couchbase","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/","url":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/","name":"Document Revisions - Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/fr\/#website"},"datePublished":"2023-06-23T08:43:43+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/fr\/developers\/tutorials\/document-revisions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/ko\/"},{"@type":"ListItem","position":2,"name":"Document Revisions"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/fr\/#website","url":"https:\/\/www.couchbase.com\/fr\/","name":"Couchbase","description":"The Operational Data Platform for AI\u2122","publisher":{"@id":"https:\/\/www.couchbase.com\/fr\/#organization"},"alternateName":"Couchbase, Inc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/fr\/#organization","name":"Couchbase","alternateName":"Couchbase, Inc.","url":"https:\/\/www.couchbase.com\/fr\/","logo":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.couchbase.com\/fr\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","contentUrl":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","width":1129,"height":1129,"caption":"Couchbase"},"image":{"@id":"https:\/\/www.couchbase.com\/fr\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Couchbase\/","https:\/\/x.com\/couchbase","https:\/\/www.youtube.com\/c\/CouchbaseServer","https:\/\/www.linkedin.com\/company\/couchbase\/","https:\/\/github.com\/couchbase\/","https:\/\/en.wikipedia.org\/wiki\/Couchbase,_Inc."],"description":"Couchbase est une soci\u00e9t\u00e9 de logiciels de bases de donn\u00e9es dont le si\u00e8ge social est situ\u00e9 \u00e0 San Jose, en Californie, et qui fournit une plateforme op\u00e9rationnelle de donn\u00e9es pour l\u2019IA utilis\u00e9e par des centaines d\u2019entreprises mondiales.","legalName":"Couchbase, Inc.","foundingDate":"2011-01-01"}]}},"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorials\/10199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/types\/tutorials"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorials\/10199\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/media?parent=10199"}],"wp:term":[{"taxonomy":"tutorial-languages","embeddable":true,"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorial-languages?post=10199"},{"taxonomy":"tutorial-tags","embeddable":true,"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorial-tags?post=10199"},{"taxonomy":"tutorial-technologies","embeddable":true,"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorial-technologies?post=10199"},{"taxonomy":"tutorial-types","embeddable":true,"href":"https:\/\/www.couchbase.com\/fr\/wp-json\/wp\/v2\/tutorial-types?post=10199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}