Databases 15 min read

Build a Simple Employee Management System with MongoDB and Flask

This tutorial walks you through creating a web‑based employee management system that demonstrates MongoDB CRUD operations using Python, Flask, and Pipenv, covering project setup, environment configuration, code implementation for querying, adding, updating, and soft‑deleting records, and how to run the application.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Build a Simple Employee Management System with MongoDB and Flask

To reinforce MongoDB insert, delete, update, and query functions, this guide builds a simple employee management system project.

1 Understand Project Goal

The final result is a web page; you only need to develop the MongoDB‑related code.

The page displays personnel information with an “Add Person” button, allowing you to view, add, edit, and delete records.

Employee management page
Employee management page

Adding Information

Click the “Add Person” button to open a dialog, fill in the fields, and the data is stored in MongoDB and shown on the page.

Editing Information

Click the “Edit” button next to a record, modify fields (except ID and age, which are derived from birthdate), then click “Update” to save.

Deleting Information

Click the “Delete” button next to a record to remove it; the automatically generated employee ID never changes after deletion.

2 Preparation

2.1 Clone Project

git clone https://github.com/sunsharing-note/MonogoDB.git

2.2 Project Structure

The project includes:

Pipfile and Pipfile.lock for environment management.

answer/DataBaseManager.py – reference implementation.

bin/generate_data.py – script to insert initial data.

main.py, static, templates, util – backend and frontend code (no need to modify).

Project file tree
Project file tree

2.3 Set Up Environment

Use Pipenv to create an isolated virtual environment and install dependencies. python3 -m pip install pipenv Enter the project folder and run: pipenv install Activate the environment:

pipenv shell

2.4 Run Project

Linux/macOS:

export FLASK_APP=main.py
flask run

Windows:

set FLASK_APP=main.py
flask run

Open http://127.0.0.1:5000 in a browser to see the page with three test records.

3 Development Process

3.1 Generate Initial Data

Run generate_data.py after starting MongoDB; it creates database chapter_4 and collection people_info with 19 initial documents.

Initial data in MongoDB
Initial data in MongoDB

3.2 Implement Query Function

Complete query_info() to return all documents where deleted equals 0, excluding _id:

def query_info(self):
    info_list = list(self.handler.find({'deleted': 0}, {'_id': 0}))
    return info_list

3.3 Implement Add Function

Logic: if the collection is empty, the new employee ID is 1; otherwise it is the current maximum ID plus 1, then insert the record.

def _query_last_id(self):
    last_info = self.handler.find({}, {'_id': 0, 'id': 1}).sort('id', -1).limit(1)
    return last_info[0]['id'] if last_info else 0

3.4 Implement Update Function

def update_info(self, people_id, para_dict):
    try:
        self.handler.update_one({'id': people_id}, {'$set': para_dict})
    except Exception as e:
        return False
    return True

3.5 Implement Delete (Soft Delete)

Soft delete is achieved by setting deleted to 1.

def del_info(self, people_id):
    return self.update_info(people_id, {'deleted': 1})

Conclusion

This project uses a Flask‑based employee management website to help readers practice fundamental MongoDB operations. By modifying DataBaseManager.py, users can see real‑time results and gain confidence in using Python for data insertion, querying, updating, and soft deletion in everyday work.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythondatabaseFlaskCRUDMongoDBEmployee Management
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.