Junior — Middle
What are the main differences between relational database management systems PostgreSQL and NoSQL database MongoDB?
sobes.tech AI
Answer from AI
PostgreSQL is a relational database management system (RDBMS) that stores data in tables with a well-defined schema (structure). It supports complex SQL queries, transactions with ACID properties, relationships between tables (via foreign keys), and ensures data integrity.
MongoDB is a NoSQL database focused on storing documents in BSON format (similar to JSON). It does not require a fixed schema, providing flexibility in data structure. MongoDB is well-suited for storing and processing large volumes of unstructured or semi-structured data.
Main differences:
- Data model: PostgreSQL — tables with relations; MongoDB — collections of documents.
- Schema: PostgreSQL — strict schema; MongoDB — flexible schema or none.
- Queries: PostgreSQL uses SQL; MongoDB has its own query language based on JSON.
- Transactions: PostgreSQL supports full transactions; MongoDB supports transactions from version 4.0, but is generally used for less strict scenarios.
- Scalability: MongoDB is easier to scale horizontally (sharding), PostgreSQL traditionally scales vertically.
Example of a query in PostgreSQL:
SELECT * FROM users WHERE age > 30;
Example of a query in MongoDB:
db.users.find({ age: { $gt: 30 } });