PouchDB Update Batch



PouchDB Update Batch

  • bulkDocs() - It is used to update an array of documents in PouchDB .
  • To update a batch, we have to create an array of documents which contains _id, _rev and the values that we need to update.
 {
id: '001',
key: '001',
value: { rev: '5-2cc59b86dde184268217fcb584dd7b5e' },
doc: {
name: 'reema',
age: 24,
      Designation: 'Teacher',
      _id: '001',
      _rev: '5-2cc59b86dde184268217fcb584dd7b5e'
    }
  },
  {
id: '002',
key: '002',
value: { rev: '1-e31c2a8d36ab9aac248fa9c168bfb5f1' },
doc: {
name: 'Ram',
age: 25,
      Designation: 'Designer',
      _id: '002',
      _rev: '1-e31c2a8d36ab9aac248fa9c168bfb5f1'
    }
  },
  {
id: '003',
key: '003',
value: { rev: '1-13ed46f3c354dde6ee6e38cca78cff0d' },
doc: {
name: 'pravin',
age: 26,
      Designation: 'Engineer',
      _id: '003',
      _rev: '1-13ed46f3c354dde6ee6e38cca78cff0d'
    }
  }
  • Let's update the documents using their individual_id and _rev values:
  • Change the name and age of all three documents:
//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('Second_Database');  
//Preparing the document  
docs = [{_id : '001', _rev:'4-f59034f061004dbca22da61662459a16', age : 21, name:  'admin', },  
      {_id : '002', _rev: ‘1 -e31c2a8d36ab9aac248fa9c168bfb5f1'', age : 26, name:  'nisha', },  
      {_id : '003', _rev: '1-13ed46f3c354dde6ee6e38cca78cff0d', age : 19, name: 'asha', }]  
//Updating the documents in bulk  
db.bulkDocs(docs, function(err, response) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log("Documents Updated Successfully");  
   }  
});  
  • Save the file name as "Update_Batch2.js " within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file.
node Update_Batch2.js  

Output

 PouchDB Update Batch

PouchDB Update Batch

Verification

  • We can verify that the batch is updated by using node (Read_Batch) command:
 PouchDB Update Batch

PouchDB Update Batch

Update Batch in Remote Database

Additionally we can update an array of documents in a database that is stored remotely on a server (CouchDB). For this purpose, You just have to pass the path to the database where you want to update the documents in CouchDB.

  • We have a database named "employees" in the CouchDB Server.
 PouchDB Update Batch

PouchDB Update Batch

  • There are three documents in "employees" database
 PouchDB Update Batch

PouchDB Update Batch

  • You can fetch these documents by using node (Read_Remote_Batch.js) command.
  • Now update all the documents name and age of "employees" database stored on CouchDB Server.
//Requiring the package   
varPouchDB = require('PouchDB');  
//Creating the database object   
vardb = new PouchDB('http://localhost:5984/employees');   
//Preparing the document   
docs = [{_id : '001', _rev:'4-1e412fc629671f09383beea7c3317393', age : 20, name: 'Reena'  },   
      {_id : '002', _rev: '1-25ce5cbb5339b299077f98e6f5baea83', age : 21,   
name: 'ram' },   
      {_id : '003', _rev: '1-4a57a26b5b02ac80bd823a18cd191df1', age : 22,   
name: 'pravin'}]  
//Inserting Document   
db.bulkDocs(docs, function(err, response) {   
if (err) {   
return console.log(err);   
   } else {   
console.log(+"Documents Updated Successfully");   
   }   
});  
  • Save the file name as "Update_Remote_Batch.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.
node Update_Remote_Batch.js  
  • It will update the documents in "employees" database.

Verification

  • You can verify that the batch is updated by using node (Read_Remote_Batch) command:
 PouchDB Update Batch

PouchDB Update Batch



Related Searches to PouchDB Update Batch