Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Learn why MongoDB was selected as a leader in the 2024 Gartner® Magic Quadrant™
MongoDB Developer
Python
plus
Sign in to follow topics
MongoDB Developer Center
chevron-right
Developer Topics
chevron-right
Languages
chevron-right
Python
chevron-right

Simple CRUD operations with Python and MongoDB

1 min read • Published May 23, 2022 • Updated May 23, 2022
FastApiPython
FULL APPLICATION
Facebook Icontwitter iconlinkedin icon
Rate this code example
star-empty
star-empty
star-empty
star-empty
star-empty
social-githubView Code
For the absolute beginner, there's nothing more simple than a CRUD tutorial. Create, Read, Update, and Delete documents using this mongodb tutorial for Python.

Introduction

To get started, first you'll need to understand that we use pymongo, our python driver, to connect your application to MongoDB. Once you've installed the driver, we'll build a simple CRUD (Create, Read, Update, Delete) application using FastAPI and MongoDB Atlas. The application will be able to create, read, update, and delete documents in a MongoDB database, exposing the functionality through a REST API.
You can find the finished application on Github here.

About the App You'll Build

This is a very basic example code for managing books using a REST API. The REST API has five endpoints:
GET /book: to list all books GET /book/<id>: to get a book by its ID POST /book: to create a new book PUT /book/<id>: to update a book by its ID DELETE /book/<id>: to delete a book by its ID
To build the API, we'll use the FastAPI framework. It's a lightweight, modern, and easy-to-use framework for building APIs. It also generates Swagger API documentation that we'll put to use when testing the application.
We'll be storing the books in a MongoDB Atlas cluster. MongoDB Atlas is MongoDB's database-as-a-service platform. It's cloud-based and you can create a free account and cluster in minutes, without installing anything on your machine. We'll use PyMongo to connect to the cluster and query data.
This application uses Python 3.6.

Facebook Icontwitter iconlinkedin icon
Rate this code example
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Implementing Robust RAG Pipelines: Integrating Google's Gemma 2 (2B) Open Model, MongoDB, and LLM Evaluation Techniques


Sep 12, 2024 | 20 min read
Quickstart

Quick Start: Getting Started With MongoDB Atlas and Python


Apr 10, 2024 | 4 min read
Tutorial

How to Use Cohere Embeddings and Rerank Modules With MongoDB Atlas


Aug 14, 2024 | 10 min read
Article

Using SuperDuperDB to Accelerate AI Development on MongoDB Atlas Vector Search


Sep 18, 2024 | 6 min read
Technologies Used
Languages
Technologies
Table of Contents
  • Introduction