PouchDB Update Document

A document in PouchDB can be updated by using the (_rev). A _rev is generated when we create a document in PouchDB. It is called revision marker. The _rev's value is a unique random number, each time we make changes to the document the value of _rev is changed.

To update a document, we have to retrieve _rev value of the document which we want to update.

Now, place the contents that are to be updated along with the retrieved _rev value in a new document, and finally insert this document in PouchDB using the put() method.


Update Document Example

First retrieve the data from the document to get its _rev number.

Use read document method.

Now use the _rev and update the value of "age" to 24. See the following code:

Save the above code in a file named "Update_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

{ age: 24,
  _id: '001',
  _rev: '2-b26971720f274f1ab7234b3a2be93c83' }
PouchDB Update document 1

Update a Document in Remote Database

You can update an existing document in a database which is stored remotely on CouchDB Server. For this, you have to pass the path of the database which contains the document that you want to update.

Example

We have a database named "employees" on the CouchDB Server.

PouchDB Update document 2

By clicking on "employees", you will find that it has a document.

PouchDB Update document 3

Let's update name and age of the document having id "001" that exists in database "employees" and stored on CouchDB Server.

Updating:

Save the above code in a file named "Update_Remote_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

{ _id: '001',
  _rev: '4-406cbc35b975d160d8814c04d64bafd3',
  name: 'Aryan',
  age: 25 }
PouchDB Update document 4

You can also see that document has been successfully changed on CouchDB server.

PouchDB Update document 5



Latest Courses