Create Document in PouchDB

Create Document To create a document in PouchDB, the db.put() method is used.

Syntax:

db.put(document, callback)

Example:

  • Create a file named “NewDocument.js” within a folder named “Examples”.
  • Add the below code to the file.NewDocument.js:
    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    doc = {
    _id: '100',
    name: 'Tom',
    salary: 45000,
    experience: 3
    }
    db.put(doc, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Document Successfully Created.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node NewDocument.js
    

 

To insert a Document in Remote Database:

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

Example:

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

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    doc = {
    _id: '100',
    name: 'Tom',
    salary: 45000,
    experience: 3
    }
    db.put(doc, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Document Successfully Created.");
    }
    });
    • Open the command prompt.
    • Execute the .js file.
      node NewRemoteDocument.js
      

     

    Verification:

    Visit the “students” database on CouchDB Server, to verify the document creation.

     

Please follow and like us:
Content Protection by DMCA.com