PouchDB Delete Batch



PouchDB Delete Batch

  • bulkDocs() - This method is used to delete an array of documents in PouchDB. We know the _id and _rev values of the documents that we need to delete from the database.
  • We have a database named "Second_Database" stored in PouchDB and contains 3 documents:
[ {
id: '001',
key: '001',
value: { rev: '4-1e412fc629671f09383beea7c3317393' },
doc: {
name: 'Admin',
age: 23,
      Designation: 'Programmer',
      _id: '001',
      _rev: '4-1e412fc629671f09383beea7c3317393'
    }
  },
  {
id: '002',
key: '002',
value: { rev: '1-25ce5cbb5339b299077f98e6f5baea83' },
doc: {
name: 'nisha',
age: 24,
      Designation: 'Teacher',
      _id: '002',
      _rev: '1-25ce5cbb5339b299077f98e6f5baea83'
    }
  },
  {
id: '003',
key: '003',
value: { rev: '1-4a57a26b5b02ac80bd823a18cd191df1' },
doc: {
name: 'Asha',
age: 25,
      Designation: 'Mechanic',
      _id: '003',
      _rev: '1-4a57a26b5b02ac80bd823a18cd191df1'
    }
 PouchDB Delete Batch

PouchDB Delete Batch

Here delete the documents using their respective _id and _rev values:

//Requiring the package  
varPouchDB = require('PouchDB');  
//Creating the database object  
vardb = new PouchDB('Second_Database');  
//Preparing the document  
docs = [{_id : '001', _rev: '4-1e412fc629671f09383beea7c3317393', _deleted : true },  
      {_id : '002', _rev: '1-25ce5cbb5339b299077f98e6f5baea83', _deleted : true },   
      {_id : '003', _rev: '1-4a57a26b5b02ac80bd823a18cd191df1', _deleted : true }]  
//Deleting Documents  
db.bulkDocs(docs, function(err, response) {  
if (err) {  
return console.log(err);  
   } else {  
console.log(response+"Documents deleted Successfully");  
   }  
}); 
  • Save the above code in a file named "Delete_Batch.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:
node Delete_Batch.js  

Output:

 PouchDB Delete Batch

PouchDB Delete Batch

  • The batch is now deleted. You can also verify it.

Read Also

Verification

 PouchDB Delete Batch

PouchDB Delete Batch

  • You can see that there is no document in database.

Delete a Batch from Remote Database

Additionally we can delete 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 delete the documents in CouchDB.

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

PouchDB Delete Batch

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

PouchDB Delete Batch

  • You can fetch these documents by using node (Read_Remote_Batch.js) command.
{
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'
    }
  }
] 
 PouchDB Delete Batch

PouchDB Delete Batch

  • Now delete all the documents of database "employees".

Read Also

//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('http://localhost:5984/employees');  
//Preparing the document  
docs = [{_id : '001', _rev: '7-7d47e310d011cd29c6cb1f3e6ee90b5e', _deleted : true },  
      {_id : '002', _rev: '2-6cfa4cd923a6d6cb2ad6368890b308f5', _deleted : true },  
      {_id : '003', _rev: '2-f85f7aa70407e7cd2c6984ccd75ed2a2', _deleted : true }]  
//Deleting Documents  
db.bulkDocs(docs, function(err, response) {  
   if (err) {  
      return console.log(err);    
   } else {  
      console.log("Documents deleted Successfully");  
   }  
});  

Save the file name as "Delete_Remote_Batch.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.

node Delete_Remote_Batch.js  

Output:

 PouchDB Delete Batch

PouchDB Delete Batch

Verification

  • You can see that there is no document in "employees"database.
 PouchDB Delete Batch

PouchDB Delete Batch



Related Searches to PouchDB Delete Batch