Read Batch in PouchDB

PouchDB Read Batch

To retrieve a PouchDB batch, the allDocs() method is used.

Syntax:

db.allDocs()

Example:

  • Create a file named “ReadBatch.js” within a folder named “Examples”.
  • Add the below code to the file.

    ReadBatch.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    db.allDocs(function(err, docs) {
    if (err) {
    return console.log(err);
    } else {
    console.log (docs.rows);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node ReadBatch.js
    

    Output:

    [ { id: '101',
    key: '101',
    value: { rev: '1-f56789f432154adb782da98358921a14' } },
    { id: '102',
    key: '102',
    value: { rev: '1-3cacb3a45e5678d90870f4e92dc9876d' } },
    { id: '103',
    key: '103',
    value: { rev: '1-afec1229a76543db973ace8fcbacd45' } } ]

     

    To see the whole document in the result:

    • Make the optional parameter include_docs true.
    • Create a file named “ReadWholeBatch.js” within a folder named “Examples”.
    • Add the below code to the file.
      ReadWholeBatch.js:

      var PouchDB = require('PouchDB');
      var db = new PouchDB('Example_Database');
      db.allDocs({include_docs: true}, function(err, docs) {
      if (err) {
      return console.log(err);
      } else {
      console.log (docs.rows);
      }
      });
    • Open the command prompt.
    • Execute the .js file.
      node ReadWholeBatch.js
      

      Output:

      [ {  id: '101',
      key: '101',
      value: { rev: '1-f56789f432154adb782da98358921a14' },
      doc:
      { name: 'Tom',
      salary: 25000,
      experience: 3,
      _id: '101',
      _rev: '1-f56789f432154adb782da98358921a14' } },
      { id: '102',
      key: '102',
      value: { rev: '1-3cacb3a45e5678d90870f4e92dc9876d' },
      doc:
      {name: 'Jerry',
      salary: 20000,
      experience: 2,
      _id: '102',
      _rev: '1-3cacb3a45e5678d90870f4e92dc9876d' } },
      { id: '103',
      key: '103',
      value: { rev: '1-afec1229a76543db973ace8fcbacd45' },
      doc:
      { name: 'Bruno',
      salary: 30000,
      experience: 4,
      _id: '103',
      _rev: '1-afec1229a76543db973ace8fcbacd45' } } ]

    To read a Batch from Remote Database:

    Instead of the database name, pass the path of the database to read a batch in CouchDB or remote server.

    Example:

    • Create a file named “ReadRemoteBatch.js” within a folder named “Examples”.
    • Add the below code to the file.
      ReadRemoteBatch.js:

      var PouchDB = require('PouchDB');
      var db = new PouchDB('http://localhost:5984/students');
      db.allDocs({include_docs: true}, function(err, docs) {
      if (err) {
      return console.log(err);
      } else {
      console.log(docs.rows);
      }
      });
    • Open the command prompt.
    • Execute the .js file.
      node ReadRemoteBatch.js
      

      Output:

      [ {  id: '101',
      key: '101',
      value: { rev: '1-acb312f4321546901aada9835889ccca1' },
      doc:
      { name: 'Tom',
      salary: 25000,
      experience: 3,
      _id: '101',
      _rev: '1-acb312f4321546901aada9835889ccca1' } },
      { id: '102',
      key: '102',
      value: { rev: '1-1a2c3b3a45ae5111d90870f4e92dc55543' },
      doc:
      {name: 'Jerry',
      salary: 20000,
      experience: 2,
      _id: '102',
      _rev: '1-1a2c3b3a45ae5111d90870f4e92dc55543' } },
      { id: '103',
      key: '103',
      value: { rev: '1-345ac229a76543db6ab5648fc987bbb5' },
      doc:
      { name: 'Bruno',
      salary: 30000,
      experience: 4,
      _id: '103',
      _rev: '1-345ac229a76543db6ab5648fc987bbb5' } } ]