PouchDB Read Batch



PouchDB Read Batch

allDocs() method in pouchdb is used to retrieve or read bulk or multiple documents from a database. This method takes a callback function as optional parameter.

Syntax

db.allDocs()

Example

usingdb.allDocs() method. 
//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('Second_Database');  
//Retrieving all the documents in PouchDB  
db.allDocs(function(err, docs) {  
   if (err) {     
      return console.log(err);  
   } else {  
      console.log (docs.rows);  
   }  
});  
  • Save the file name as "read_batch.js " within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:
node read_batch.js  

Output

 PouchDB Read Batch

PouchDB Read Batch

Generally, allDocs() method we can see only the _id, key and _rev fields of each document. If we need to see the whole document in the result, you have to make the optional parameter include_docs true.

usingallDocs() method 
//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('Second_Database');  
//Retrieving all the documents in PouchDB  
db.allDocs({include_docs: true}, function(err, docs) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log (docs.rows);  
   }  
});  
  • Save the file name as "Read_Batch2.js " within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file.
node Read_Batch2.js  

Output

 PouchDB Read Batch

PouchDB Read Batch

Read a Batch from Remote Database

We can read a batch from a database which is stored remotely on CouchDB Server. For this purpose, we have to pass the path of the database where you need to read the batch.

  • We have a database named "employees" on the CouchDB Server.
 PouchDB Read Batch

PouchDB Read Batch

 PouchDB Read Batch

PouchDB Read Batch

  • Let's read all the documents from the "employees" database stored on CouchDB Server.
//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('http://localhost:5984/employees');  
//Retrieving all the documents in PouchDB  
db.allDocs({include_docs: true}, function(err, docs) {  
   if (err) {  
      return console.log(err);  
   } else {  
      console.log(docs.rows);  
   }  
});  
  • Open the command prompt and execute the JavaScript file using node:
node Read_Remote_Batch.js  

Output:

 PouchDB Read Batch

PouchDB Read Batch



Related Searches to PouchDB Read Batch