PouchDB Update Document
PouchDB Update Document
- A document in PouchDB can be updated by using the (_rev). A _rev is created when we make a document in PouchDB. It is called revision marker.
- The _rev's value includes a unique random number. To update a document, you have to recover _rev value of the document which we need to update.
- Now, place the contents that are to be updated along with the retrieved _rev value in a new document, and finally embed this document in PouchDB using the put() method.
Update Document Example
- To retrieve the data form the document, first you need to get _rev number.
{ _id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f' }
name: 'Admin',
age: 28,
designation: 'Developer' }
- Now use the _rev and update the value of "age" to 25. See the following code:
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Preparing the document for update
doc = {
age: 25,
_id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f'
}
//Inserting Document
db.put(doc);
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
- Save the file named as "Update_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and Execute the JavaScript file:
node Update_Document.js
Output:
{ age: 25,
_id: '001',
_rev: '2-2da06e94be54c8390eb02435a15e9a68' }

PouchDB Update Document
Update a Document in Remote Database
You can also update the existing document in a remotely stored database(CouchDB). For this purpose, You just pass the path of the database where you want to update the documents in CouchDB.
Example
- We have a database named "employees" on the CouchDB Server.

PouchDB Update Document
- By clicking on "employees", you will find that it has a document.

PouchDB Update Document
- Let's update name and age of the document having id "001" that exists in database "employees" and stored on CouchDB Server.
Updating:
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/employees');
//Preparing the document for update
doc = {
"_id": "001",
"_rev": "3-276c137672ad71f53b681feda67e65b1",
"name": "aryan",
"age": 25
}
//Inserting Document
db.put(doc);
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
- 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:
node Update_Remote_Document.js
Read Also
Output:
{ _id: '001',
_rev: '4-406cbc35b975d160d8814c04d64bafd3',
name: 'aryan',
age: 25 }
- You can also see that document has been successfully changed on CouchDB server.
