Home » Pouchdb Update Batch

Pouchdb Update Batch

by Online Tutorials Library

PouchDB Update Batch

The bulkDocs() method is used to update an array of documents in PouchDB. To update a batch, you have to create an array of documents where, each document contains _id, _rev and the values that you want to update.

We have a database named “Second_Database” stored in PouchDB and contains 3 documents:

Let’s update the documents using their respective _id and _rev values:

Change the name and age of all three documents:

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

Output:

PouchDB Update batch 1


Verification

You can verify that the batch is updated by using node (Read_Batch) command:

PouchDB Update batch 2


Update Batch in Remote Database

We can also update an array of documents in a database that is stored remotely on a server (CouchDB). You just have to pass the path to the database in CouchDB, which contains the document that you want to update.

Example

We have a database named “employees” in the CouchDB Server.

PouchDB Update batch 3

There are three documents in “employees” database

PouchDB Update batch 4

You can fetch these documents by using node (Read_Remote_Batch.js) command.

PouchDB Update batch 5

Now update all the documents name and age of “employees” database stored on CouchDB Server.

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

It will update the documents in “employees” database.


Verification

You can verify that the batch is updated by using node (Read_Remote_Batch) command:

PouchDB Update batch 6

You may also like