PouchDB Create Document

The db.put() method is used to create a document in PouchDB database. The document that is created in PouchDB database is stored in a variable and pass as a parameter to this method. This method can also accepts a callback (optional) function as a parameter.

Syntax:


Create Document Example

Let's first create a database named "Second_Database" in PouchDB because we have deleted the previous one.

Visit: How to create database in PouchDB

Use put() method to create a document. The created document should be in JSON format, a set of key-value pairs separated by comma (,) and enclosed within curly braces ({}).

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

PouchDB Create document 1

Insert a Document in Remote Database

You can also insert a document in a remotely stored database (CouchDB). You just have to pass the path of the database where you want to create documents in CouchDB, instead of the database name.


Insert a Document in Remote Database Example

We have a database named "employees" on CouchDB. This database has no documents within it as you can see in below image:

PouchDB Create document 2

Let's see how to insert a document in the database named "employees" saved on CouchDB server.

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

PouchDB Create document 3

Verification

You can verify that the document is created by visiting the "employees" database on CouchDB Server.

PouchDB Create document 4
PouchDB Create document 5



Latest Courses