Skip to main content
Course/Module 3/Topic 3 of 3Intermediate

CRUD Operations

Master Create, Read, Update, and Delete operations with both MongoDB and PostgreSQL

45 minBy Priygop TeamLast updated: Feb 2026

CRUD Operations with Mongoose

Example
// Create (C)
const createUser = async (userData) => {
    try {
        const user = new User(userData);
        const savedUser = await user.save();
        return savedUser;
    } catch (error) {
        throw error;
    }
};

// Read (R)
const getAllUsers = async () => {
    try {
        const users = await User.find();
        return users;
    } catch (error) {
        throw error;
    }
};

const getUserById = async (id) => {
    try {
        const user = await User.findById(id);
        return user;
    } catch (error) {
        throw error;
    }
};

const getUserByEmail = async (email) => {
    try {
        const user = await User.findOne({ email });
        return user;
    } catch (error) {
        throw error;
    }
};

// Update (U)
const updateUser = async (id, updateData) => {
    try {
        const user = await User.findByIdAndUpdate(id, updateData, { new: true });
        return user;
    } catch (error) {
        throw error;
    }
};

// Delete (D)
const deleteUser = async (id) => {
    try {
        const user = await User.findByIdAndDelete(id);
        return user;
    } catch (error) {
        throw error;
    }
};

// Advanced queries
const getUsersByAge = async (minAge, maxAge) => {
    try {
        const users = await User.find({
            age: { $gte: minAge, $lte: maxAge }
        });
        return users;
    } catch (error) {
        throw error;
    }
};

const searchUsers = async (searchTerm) => {
    try {
        const users = await User.find({
            $or: [
                { name: { $regex: searchTerm, $options: 'i' } },
                { email: { $regex: searchTerm, $options: 'i' } }
            ]
        });
        return users;
    } catch (error) {
        throw error;
    }
};

CRUD Operations with Sequelize

Example
// Create (C)
const createUser = async (userData) => {
    try {
        const user = await User.create(userData);
        return user;
    } catch (error) {
        throw error;
    }
};

// Read (R)
const getAllUsers = async () => {
    try {
        const users = await User.findAll();
        return users;
    } catch (error) {
        throw error;
    }
};

const getUserById = async (id) => {
    try {
        const user = await User.findByPk(id);
        return user;
    } catch (error) {
        throw error;
    }
};

const getUserByEmail = async (email) => {
    try {
        const user = await User.findOne({ where: { email } });
        return user;
    } catch (error) {
        throw error;
    }
};

// Update (U)
const updateUser = async (id, updateData) => {
    try {
        const user = await User.findByPk(id);
        if (user) {
            await user.update(updateData);
            return user;
        }
        return null;
    } catch (error) {
        throw error;
    }
};

// Delete (D)
const deleteUser = async (id) => {
    try {
        const user = await User.findByPk(id);
        if (user) {
            await user.destroy();
            return user;
        }
        return null;
    } catch (error) {
        throw error;
    }
};

// Advanced queries
const { Op } = require('sequelize');

const getUsersByAge = async (minAge, maxAge) => {
    try {
        const users = await User.findAll({
            where: {
                age: {
                    [Op.between]: [minAge, maxAge]
                }
            }
        });
        return users;
    } catch (error) {
        throw error;
    }
};

const searchUsers = async (searchTerm) => {
    try {
        const users = await User.findAll({
            where: {
                [Op.or]: [
                    { name: { [Op.iLike]: `%${searchTerm}%` } },
                    { email: { [Op.iLike]: `%${searchTerm}%` } }
                ]
            }
        });
        return users;
    } catch (error) {
        throw error;
    }
};

Try It Yourself — Database Integration

Try It Yourself — Database IntegrationHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|39 lines|1938 chars|✓ Valid syntax
UTF-8

Quick Quiz — Database Integration

Additional Resources

Recommended Reading

  • MongoDB Documentation
  • Mongoose ODM Guide
  • PostgreSQL Documentation
  • Sequelize ORM Documentation

Online Resources

  • MongoDB with Node.js Tutorial
  • PostgreSQL with Sequelize Guide
  • Database Design Best Practices
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep