All Course > Data Science With Fastapi > Database and data storage for fastapi applications Nov 16, 2024

FastAPI MongoDB Tutorial: Store JSON Data with NoSQL

In the previous lesson, we explored how to connect FastAPI with PostgreSQL, a relational database, to handle structured data. We learned how to set up the database, create tables, and perform CRUD operations using SQLAlchemy. Now, we’ll shift our focus to NoSQL databases, specifically MongoDB, which is perfect for handling unstructured data like JSON. This lesson will guide you through connecting FastAPI to MongoDB, storing and retrieving JSON data, and optimizing your app for NoSQL queries.

Why Use MongoDB with FastAPI?

I recently worked on a project where I needed to store user activity logs, which had varying structures. Some logs had only a timestamp and an action, while others included additional metadata like location or device info. Using a relational database like PostgreSQL would have forced me to define a rigid schema, which wasn’t practical. That’s when I decided to use MongoDB, a NoSQL database that allows flexible data storage.

MongoDB stores data in BSON (Binary JSON) format, making it ideal for applications that deal with unstructured or semi-structured data. FastAPI, with its async capabilities, pairs perfectly with MongoDB to handle high-speed data operations. Together, they enable you to build scalable and efficient data science applications.

Connecting FastAPI to MongoDB

To get started, you’ll need to install the required libraries. Use the following command to install motor, an async MongoDB driver for Python:

pip install motor 

Next, set up the connection to your MongoDB instance. Here’s how I did it in my project:

from fastapi import FastAPI  
from motor.motor_asyncio import AsyncIOMotorClient  

app = FastAPI()  

# MongoDB connection URL  
MONGO_URL = "mongodb://localhost:27017"  

@app.on_event("startup")  
async def startup_db_client():  
    app.mongodb_client = AsyncIOMotorClient(MONGO_URL)  
    app.mongodb = app.mongodb_client["mydatabase"]  

@app.on_event("shutdown")  
async def shutdown_db_client():  
    app.mongodb_client.close()  

In this code, we use AsyncIOMotorClient to connect to MongoDB. The startup event ensures the connection is established when the app starts, and the shutdown event closes the connection when the app stops.

Storing and Retrieving JSON Data

Once the connection is set up, you can start storing and retrieving JSON data. Let’s say you want to store user activity logs. Here’s how I implemented it:

from fastapi import HTTPException  

@app.post("/log")  
async def create_log(log: dict):  
    if not log:  
        raise HTTPException(status_code=400, detail="No log data provided")  
    result = await app.mongodb["logs"].insert_one(log)  
    return {"id": str(result.inserted_id)}  

@app.get("/log/{log_id}")  
async def get_log(log_id: str):  
    log = await app.mongodb["logs"].find_one({"_id": log_id})  
    if log:  
        return log  
    raise HTTPException(status_code=404, detail="Log not found")  

In this example, the /log endpoint accepts a JSON object and stores it in the logs collection. The /log/{log_id} endpoint retrieves a log by its ID. MongoDB automatically generates a unique _id for each document, which makes it easy to query data.

Optimizing FastAPI for NoSQL Queries

To optimize your FastAPI app for NoSQL queries, consider the following tips:

  • Indexing: Create indexes on fields you frequently query. For example, if you often search logs by user ID, create an index on the user_id field.

  • Projection: Use projection to retrieve only the fields you need. This reduces the amount of data transferred and improves performance.

  • Aggregation: Use MongoDB’s aggregation framework to perform complex queries efficiently.

Here’s an example of indexing in MongoDB:

await app.mongodb["logs"].create_index("user_id")

Conclusion

In this tutorial, we explored how to use MongoDB with FastAPI to store and retrieve JSON data. We covered connecting FastAPI to MongoDB, performing CRUD operations, and optimizing NoSQL queries. MongoDB’s flexibility makes it a great choice for handling unstructured data, and FastAPI’s async capabilities ensure high performance.

If you’re ready to take your skills to the next level, check out the next lesson on adding authentication and security to FastAPI. This will help you build secure and robust data science applications.

Comments

There are no comments yet.

Write a comment

You can use the Markdown syntax to format your comment.