Read Document in PouchDB

Read Document

To retrieve the document created in PouchDB, the db.get() method is used.

Syntax:

db.get(document, callback)

Example:

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

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    db.get('100', function(err, doc) {
    if (err) {
    return console.log(err);
    } else {
    console.log(doc);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node ReadDocument.js
    

    Output:

    { name: 'Tom',
    salary: 45000,
    experience: 3,
    _id: '100',
    _rev: '1-99a7a70ec2a34678845637a16d57344f' }

To read a Document from the Remote Database:

Instead of the database name, pass the path of the database in order to retrieve a document from CouchDB or remote server.

Example:

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

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    db.get('100', function(err, doc) {
    if (err) {
    return console.log(err);
    } else {
    console.log(doc);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node ReadRemoteDocument.js
    

    Output:

    { _id: '100',
    _rev: '3-897c876543ad71f53b345feda12e65b1',
    name: 'Tom',
    salary: 45000,
    experience: 3 }