Notice: Constant WP_MEMORY_LIMIT already defined in /home/javawith/public_html/codesjava.com/wp-config.php on line 107

Notice: Constant WP_MAX_MEMORY_LIMIT already defined in /home/javawith/public_html/codesjava.com/wp-config.php on line 108
Create Batch in PouchDB - CodesJava

CodesJava

Easy learning with example program codes

Create Batch in PouchDB


Notice: Undefined variable: footnotelist in /home/javawith/public_html/codesjava.com/wp-content/plugins/latex2html/inc/core.php on line 475

PouchDB Create Batch
An array of documents is termed as Batch in PouchDB. To create a PouchDB batch The db.bulkDocs() method is used. If there are no unique _id values for each document then, PouchDB generates unique ids in bulk for all the documents.

Syntax:

db.bulkDocs(docs, [options], [callback])

Example:

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

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    doc1 = {_id: '101', name: 'Tom', salary: 25000, experience: 3}
    doc2 = {_id: '102', name: 'Jerry', salary: 20000, experience: 2}
    doc3 = {_id: '103', name: 'Bruno', salary: 30000, experience: 4}
    docs = [doc1, doc2, doc3]
    db.bulkDocs(docs, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Batch Successfully Created.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node NewBatch.js
    

    Console Output:

    Batch Successfully Created.

Explanation:
The documents ‘doc1’, ‘doc2’ and ‘doc3’ are created in bulk to form an array or a Batch in the ‘Example_Database’ in PouchDB.

To create a Batch in Remote Database:

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

Example:

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

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    doc1 = {_id: '101', name: 'Tom', salary: 25000, experience: 3}
    doc2 = {_id: '102', name: 'Jerry', salary: 20000, experience: 2}
    doc3 = {_id: '103', name: 'Bruno', salary: 30000, experience: 4}
    docs = [doc1, doc2, doc3]
    db.bulkDocs(docs, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Batch Successfully Created.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node NewRemoteBatch.js
    

    Console Output:

    Batch Successfully Created.
    

Explanation:
The documents ‘doc1’, ‘doc2’ and ‘doc3’ are remotely created in bulk to form an array or a Batch in the 'students' database in CouchDB.

Verification:

Check the created batch on the CouchDB server.

 

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status