Last modified: December 03, 2024

This article is written in: 🇺🇸

CRUD in SQL vs NoSQL

Comparing common CRUD operations in SQL (relational databases) and MongoDB (a NoSQL document store) helps understand the differences between relational and non-relational databases.

CRUD Operations Comparison

Create

In SQL, you use the INSERT INTO statement to add new records to a table.

In MongoDB, you use the insertOne or insertMany method to add documents to a collection.

Read

In SQL, the SELECT statement retrieves data from one or more tables.

In MongoDB, the find method retrieves documents from a collection that match a query.

Update

In SQL, the UPDATE statement modifies existing records in a table.

In MongoDB, the updateOne or updateMany method modifies existing documents in a collection.

Delete

In SQL, the DELETE FROM statement removes records from a table.

In MongoDB, the deleteOne or deleteMany method removes documents from a collection.

CRUD Operations Table

Operation SQL Syntax SQL Example MongoDB Syntax MongoDB Example
Create INSERT INTO table_name (columns) VALUES (values); INSERT INTO users (first_name, last_name, age) VALUES ('John', 'Doe', 25); db.collection.insertOne(document); db.users.insertOne({first_name: 'John', last_name: 'Doe', age: 25});
Read SELECT columns FROM table_name WHERE condition; SELECT first_name, last_name, age FROM users WHERE age = 25; db.collection.find(query, projection); db.users.find({age: 25}, {first_name: 1, last_name: 1, age: 1});
Update UPDATE table_name SET column = value WHERE condition; UPDATE users SET age = 26 WHERE first_name = 'John' AND last_name = 'Doe'; db.collection.updateOne(filter, update); db.users.updateOne({first_name: 'John', last_name: 'Doe'}, {$set: {age: 26}});
Delete DELETE FROM table_name WHERE condition; DELETE FROM users WHERE first_name = 'John' AND last_name = 'Doe'; db.collection.deleteOne(filter); db.users.deleteOne({first_name: 'John', last_name: 'Doe'});

Best Practices

Table of Contents

    CRUD in SQL vs NoSQL
    1. CRUD Operations Comparison
      1. Create
      2. Read
      3. Update
      4. Delete
    2. CRUD Operations Table
    3. Best Practices