Skip to content

MongoDB Vs PostgreSQL Differences AQSA Spark By {Examples}

  • by

MongoDB and PostgreSQL are both powerful database management systems hence, it is important to know the difference between MongoDB vs PostgreSQL. Here, I will explain the differences by exploring the CRUD operation on MongoDB and PostgreSQL. CRUD is to create, read, update, and delete.

Related: MongoDB Beginners Tutorial with Examples

In simple words, MongoDB is a NO-SQL document-oriented database that is flexible for the querying of documents, including support for embedded documents and arrays, and PostgreSQL is a SQL database that is powerful and expressive and supports a wide range of features including advanced querying, and joins.

MongoDBPostgreSQLDatabaseDatabaseCollectionTableDocumentRow or Record

CRUD Operations in MongoDB vs PostgreSQL

In both these databases, we can perform CRUD (Create, Read, Update, Delete) operations but the syntax differs.

CRUDMongoDBPostgreSQLCreatedb.createCollection()CREATE TABLE <TABLE>Insertdb.collection.insertMany()INSERT INTO <TABLE>Selectdb.collection.find()SELECT * FROM <TABLE>Updatedb.collection.updateOne()UPDATE <TABLE>Deletedb.collection.deleteOne()DELETE FROM <TABLE>PostgreSQL vs MongoDB

1. Create in MongoDB Vs PostgreSQL

In MongoDB and PostgreSQL, new database objects like collections or tables are created using the CREATE command. We can create the collection in MongoDB using the following command.

# Create the collection in MongoDB
db.createCollection(“STUDENT”);

The above command creates a new collection named STUDENT in MongoDB. A collection holds multiple documents, it is similar to a table in RDBMS.

On the other hand, use the create table SQL statement to create a new PostgreSQL table.

# Create a new PostgreSQL table
CREATE TABLE STUDENT(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL
);

Thus, it creates a new table with the specified columns and data types shown in the output.

2. Insert in MongoDB Vs PostgreSQL

Next, insert the documents or rows in MongoDB and PostgreSQL using the insert command. The INSERT command in MongoDB is used to insert documents into a collection, while in PostgreSQL it is used to insert rows into a table. However, the syntax and usage of the command differ between the two databases. Consider the syntax for both databases.

In MongoDB, the insertOne() or insertMany() method is used to insert the documents according to the requirement. Here, we use the insertMany() method to add four documents to MongoDB at once.

# insertMany() to add documents to MongoDB
db.STUDENT.insertMany([
{
_id: 1,
name: “Emily”,
age: 22

},
{
_id: 2,
name: “Ellen”,
age: 25
},
{
_id: 3,
name: “Tyler”,
age: 21
},
{
_id: 4,
name: “Ben”,
age: 23
}
]
)

The output shows that the command inserts a new document into the specified collection with the given key-value pair.

In case, you want to insert a document only when it does not exist refer to MongoDB insert if not exists

Now in PostgreSQL, we insert multiple rows into a table using the INSERT INTO command with a comma-separated list of value sets, like shown here.

# Insert into postgreSQL table
INSERT INTO STUDENT (ID,NAME,AGE) VALUES (1, ‘Emily’, 22),
(2, ‘Ellen’, 25), (3, ‘Tyler’, 21), (4, ‘Ben’, 23);

This will insert four rows into the table as displayed in the output.

3. Retrieve data in MongoDB Vs PostgreSQL

We can retrieve data from a collection or table using the find() and SELECT commands respectively. However, the syntax and usage of these commands can differ significantly between the two databases due to their different data models and query languages.

The MongoDB find() command is used for the retrieval of data in MongoDB which is provided below.

# find() command in MongoDB
db.STUDENT.find()

As we can see, it returns all the documents from the collection. Moreover, we can also use various query operators to filter the results.

PostgreSQL uses the SELECT command to retrieve the data from the tables. Here is the command of SELECT which returns rows from the table.

# Select command in PostgreSQL
SELECT * from STUDENT;

The output displayed the rows of the specified table.

4. Update in MongoDB Vs PostgreSQL

Both MongoDB and PostgreSQL provide the ability to update data in their databases but with some differences. In MongoDB, the documents are updated via updateOne() and updateMany() commands. We have given an update command below which is performed using the $set operator.

# updateOne() method
db.STUDENT.updateOne({‘name’:’Anna’},
   {$set:{‘name’:’Emily’}})

Here, the outcome of the document modification is seen.

On the other hand, updates are performed using the UPDATE statement, which can be used to update one or many rows in a table in PostgreSQL. The SET clause with the UPDATE command is used to set the new values of the columns being updated.

# Update command in PostgreSQL
UPDATE STUDENT SET NAME = ‘Bella’ WHERE ID = 2;

Hence, the output generated the results of the update statement below.  

5. Delete in MongoDB Vs PostgreSQL

In MongoDB, deletions are performed using the deleteOne() to delete single documents or deleteMany() to delete one or many documents. Although, we can use the remove() method to do this.  The delete operation is performed below.

# deleteOne() in MongoDB
db.STUDENT.deleteOne({ “_id”: 3 })

Hence, the specified document is deleted below.

On the contrary, PostgreSQL uses the delete statement to perform deletion operations. The statement of delete is provided as follows.

# Delete Statement in PostgreSQL
DELETE FROM STUDENT WHERE ID = 3;

The specified row is now deleted from the table.

6. Conclusion

In conclusion, Both the MongoDB and PostgreSQL system has their strength and weakness, the choice between the two depends on the specific needs of our application and the type of data we are working with. I hope you now understand the difference between PostgreSQL vs MongoDB and how to perform CRUD operations on each database.

More details about this topic can be found here.

 MongoDB and PostgreSQL are both powerful database management systems hence, it is important to know the difference between MongoDB vs PostgreSQL. Here, I will explain the differences by exploring the CRUD operation on MongoDB and PostgreSQL. CRUD is to create, read, update, and delete. Related: MongoDB Beginners Tutorial with Examples In simple words, MongoDB is a NO-SQL document-oriented  Read More MongoDB 

Leave a Reply

Your email address will not be published. Required fields are marked *