PouchDB Create Batch

Batch is an array of documents in PouchDB. The db.bulkDocs() method is used to create an array of documents or batch. While creating documents, if you don't provide _id values then on your behalf PouchDB generates unique ids for all the documents in the bulk.

You can store all the documents that you want to create in PouchDB in an array and pass it to this method as a parameter. This method also accepts a callback (optional) function as a parameter.

Syntax:


Create Batch Example

Let's create multiple documents in PouchDB using the db.bulkDocs () method. The documents should be in JSON format, a set of key-value pairs separated by comma (,) and enclosed within curly braces ({}).

The batch will be created in database named "Second_Database" stored on PouchDB server.

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

Output:

PouchDB Create batch 1

Create a Batch in Remote Database

You can create a batch in a database which is stored remotely on CouchDB Server. For this, you have to pass the path of the database where you want to create the batch.

Example

We have a database named "employees" on the CouchDB Server.

PouchDB Create batch 2

There is no document in "employees" database.

PouchDB Create batch 3

Let's create a batch in "employee" database.

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

Output:

PouchDB Create batch 4

Verification

You can see the created documents on CouchDB server.

PouchDB Create batch 5



Latest Courses