Javatpoint Logo
Javatpoint Logo

PouchDB Read Batch

In PouchDB, the allDocs() method is used to read or retrieve multiple or bulk documents from a database .

This method accepts an optional callback function.

Syntax:


Read Batch Example

Let's take an example to retrieve all the documents from a database named "Second_Database" stored in PouchDB server by using db.allDocs() method. This method retrieves the batch in the form of objects.

Save the above code in a file named "Read_Batch.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

[ { id: '001',
    key: '001',
    value: { rev: '4-f59034f061004dbca22da61662459a16' } },
  { id: '002',
    key: '002',
    value: { rev: '1-0c0628a46e404d90870f4e892dc2d900' } },
  { id: '003',
    key: '003',
    value: { rev: '1-1dc4fe229a61420db2b657e8fcbbfa7d' } } ]
PouchDB Read Batch 1

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

Save the above code in a file named "Read_Batch2.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

[ { id: '001',
    key: '001',
    value: { rev: '4-f59034f061004dbca22da61662459a16' },
    doc:
     { name: 'Ajeet',
       age: 23,
       Designation: 'Programmer',
       _id: '001',
       _rev: '4-f59034f061004dbca22da61662459a16' } },
  { id: '002',
    key: '002',
    value: { rev: '1-0c0628a46e404d90870f4e892dc2d900' },
    doc:
     { name: 'Robert',
       age: 24,
       Designation: 'Teacher',
       _id: '002',
       _rev: '1-0c0628a46e404d90870f4e892dc2d900' } },
  { id: '003',
    key: '003',
    value: { rev: '1-1dc4fe229a61420db2b657e8fcbbfa7d' },
    doc:
     { name: 'Abdul',
       age: 25,
       Designation: 'Mechanic',
       _id: '003',
       _rev: '1-1dc4fe229a61420db2b657e8fcbbfa7d' } } ]
PouchDB Read Batch 2

Read a Batch from Remote Database

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

Example

We have a database named "employees" on the CouchDB Server.

PouchDB Read Batch 3
PouchDB Read Batch 4

Let's read all the documents from the "employees" database stored on CouchDB Server.

Save the above code in a file named "Read_Remote_Batch.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

Output:

[ { id: '001',
    key: '001',
    value: { rev: '6-bc6904f9fdcf64e8bc181e7da006db45' },
    doc:
     { _id: '001',
       _rev: '6-bc6904f9fdcf64e8bc181e7da006db45',
       name: 'Lucky',
       age: 24,
       Designation: 'Teacher' } },
  { id: '002',
    key: '002',
    value: { rev: '1-415437b4fd54d2af100e2eef26e3fad7' },
    doc:
     { _id: '002',
       _rev: '1-415437b4fd54d2af100e2eef26e3fad7',
       name: 'Raman',
       age: 25,
       Designation: 'Designer' } },
  { id: '003',
    key: '003',
    value: { rev: '1-ab07c37012e837c1654c0d6b9f22a56f' },
    doc:
     { _id: '003',
       _rev: '1-ab07c37012e837c1654c0d6b9f22a56f',
       name: 'Albert',
       age: 26,
       Designation: 'Engineer' } } ]
PouchDB Read Batch 5



Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA