Update Batch in PouchDB

PouchDB Update Batch To update a batch in PouchDB, the bulkDocs() method is used.

Example:

  • Retrieve the values of a bulk document. 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' } } ]
  • Using the respective _id and _rev values of the document to update them.
  • Create a file named “UpdateBatch.js” within a folder named “Examples”.
  • Add the below code to the file. UpdateBatch.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    docs = [{_id : '101', _rev:'1-f56789f432154adb782da98358921a14', salary : 30000, name:  'Smith' },
    {_id : '102', _rev: '1-3cacb3a45e5678d90870f4e92dc9876d', salary : 25000, name:  'John' },
    {_id : '103', _rev: '1-afec1229a76543db973ace8fcbacd45', salary : 35000, name: 'Holmes' }]
    db.bulkDocs(docs, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Batch Successfully Updated.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node UpdateBatch.js
    

    Console Output:

    Batch Successfully Updated.

Verification:

Verify the update by reading batch again:

Output:

[ { id: '101',
key: '101',
value: { rev: '2-a456ba2f9901a408e98406c424f390a23' },
doc:
{  salary : 30000,
name:  'Smith',
_id: '101',
_rev: '2-a456ba2f9901a408e98406c424f390a23' } },
{ id: '102',
key: '102',
value: { rev: '2-5cc4bbcd994d4a9cb0e4376cc5acaba56' },
doc:
{ salary : 25000,
name:  'John',
_id: '102',
_rev: '2-5cc4bbcd994d4a9cb0e4376cc5acaba56' } },
{ id: '103',
key: '103',
value: { rev: '2-345acb54a8eb4181b17f5f6c9c2b5b6c7' },
doc:
{ salary : 35000,
name: 'Holmes',
_id: '103',
_rev: '2-345acb54a8eb4181b17f5f6c9c2b5b6c7' } } ]

To update Batch in Remote Database:

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

  • Retrieve the values of a bulk document. 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' } } ]
  • Using the respective _id and _rev values of the document to update them.
  • Create a file named “UpdateRemoteBatch.js” within a folder named “Examples”.
  • Add the below code to the file. UpdateRemoteBatch.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    docs = [{_id : '101', _rev:'1-345ac229a76543db6ab5648fc987bbb5', salary : 30000, name:  'Smith' },
    {_id : '102', _rev:'1-1a2c3b3a45ae5111d90870f4e92dc55543', salary : 25000, name:  'John' },
    {_id : '103', _rev: '1-acb312f4321546901aada9835889ccca1', salary : 35000, name: 'Holmes' }]
    db.bulkDocs(docs, function(err, response) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Batch Successfully Updated.");
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node UpdateRemoteBatch.js
    

    Console Output:

    Batch Successfully Updated.

Verification:

Verify the update by reading batch again:

Output:

[ { id: '101',
key: '101',
value: { rev: '2-b443aabf965ba789406c424f344455661' },
doc:
{  salary : 30000,
name:  'Smith',
_id: '101',
_rev: '2-b443aabf965ba789406c424f344455661' } },
{ id: '102',
key: '102',
value: { rev: '2-567a2354a8eaca6b4181b132456accbba' },
doc:
{ salary : 25000,
name:  'John',
_id: '102',
_rev: '2-567a2354a8eaca6b4181b132456accbba' } },
{ id: '103',
key: '103',
value: { rev: '2-d321abc54a8eb4181b17f5f6c9c27845cac' },
doc:
{ salary : 35000,
name: 'Holmes',
_id: '103',
_rev: '2-d321abc54a8eb4181b17f5f6c9c27845cac' } } ]
Please follow and like us:
Content Protection by DMCA.com