|
1 | 1 | # Python FastAPI MongoDB Sample MFlix Application |
2 | 2 |
|
3 | | -[TODO: Add intro] |
| 3 | +This is a full-stack movie browsing application built with Python FastAPI and Next.js, demonstrating MongoDB operations using the `sample_mflix` dataset. The application showcases CRUD operations, aggregations, and MongoDB Search using the PyMongo driver. |
| 4 | + |
| 5 | +## Project Structure |
4 | 6 |
|
5 | 7 | ``` |
6 | 8 | ├── README.md |
7 | | -├── client/ # Next.js frontend |
8 | | -└── server/ # Python FastAPI backend |
| 9 | +├── client/ # Next.js frontend (TypeScript) |
| 10 | +└── server/ # Python FastAPI backend |
| 11 | + ├── src/ |
| 12 | + ├── tests/ |
| 13 | + ├── .env.example |
| 14 | + ├── main.py |
| 15 | + ├── pytest.ini |
| 16 | + ├── requirements.in |
| 17 | + └── requirements.txt |
9 | 18 | ``` |
10 | 19 |
|
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- **Python 3.10** to **Python 3.13** |
| 23 | +- **Node.js 20** or higher |
| 24 | +- **MongoDB Atlas cluster or local deployment** with the `sample_mflix` dataset loaded |
| 25 | + - [Load sample data](https://www.mongodb.com/docs/atlas/sample-data/) |
| 26 | +- **pip** for Python package management |
| 27 | +- **Voyage AI API key** (For MongoDB Vector Search) |
| 28 | + - [Get a Voyage AI API key](https://www.voyageai.com/) |
| 29 | + |
11 | 30 | ## Getting Started |
12 | 31 |
|
13 | | -[TODO: Add getting started instructions explaining how to set connection string, start server, start client, etc.] |
| 32 | +### 1. Configure the Backend |
| 33 | + |
| 34 | +Navigate to the Python FastAPI server directory: |
| 35 | + |
| 36 | +```bash |
| 37 | +cd server/ |
| 38 | +``` |
| 39 | + |
| 40 | +Create a `.env` file from the example: |
| 41 | + |
| 42 | +```bash |
| 43 | +cp .env.example .env |
| 44 | +``` |
| 45 | + |
| 46 | +Edit the `.env` file and set your MongoDB connection string: |
| 47 | + |
| 48 | +```env |
| 49 | +# MongoDB Configuration |
| 50 | +MONGO_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/sample_mflix?retryWrites=true&w=majority |
| 51 | +MONGO_DB=sample_mflix |
| 52 | +
|
| 53 | +# Voyage AI Configuration |
| 54 | +# API key for Voyage AI embedding model (required for Vector Search) |
| 55 | +VOYAGE_API_KEY=your_voyage_api_key |
| 56 | +
|
| 57 | +# CORS Configuration |
| 58 | +# Comma-separated list of allowed origins for CORS |
| 59 | +CORS_ORIGINS=http://localhost:3000,http://localhost:8000 |
| 60 | +``` |
| 61 | + |
| 62 | +**Note:** Replace `username`, `password`, and `cluster` with your actual MongoDB Atlas |
| 63 | +credentials. |
| 64 | + |
| 65 | +Make a virtual environment: |
| 66 | + |
| 67 | +```bash |
| 68 | +python -m venv .venv |
| 69 | +``` |
| 70 | + |
| 71 | +Activate the virtual environment: |
| 72 | + |
| 73 | +```bash |
| 74 | +source .venv/bin/activate |
| 75 | +``` |
| 76 | + |
| 77 | +Install Python dependencies: |
| 78 | + |
| 79 | +```bash |
| 80 | +pip install -r requirements.txt |
| 81 | +``` |
| 82 | + |
| 83 | +### 2. Start the Backend Server |
| 84 | + |
| 85 | +From the `server/` directory, run: |
| 86 | + |
| 87 | +```bash |
| 88 | +fastapi dev main.py --reload |
| 89 | +``` |
| 90 | + |
| 91 | +The server will start on `http://localhost:8000`. You can verify it's running by visiting: |
| 92 | +- API root: http://localhost:8000/api/movies |
| 93 | +- API documentation (Swagger UI): http://localhost:8000/docs |
| 94 | + |
| 95 | +### 3. Configure and Start the Frontend |
| 96 | + |
| 97 | +Open a new terminal and navigate to the client directory: |
| 98 | + |
| 99 | +```bash |
| 100 | +cd client |
| 101 | +``` |
| 102 | + |
| 103 | +Install dependencies: |
| 104 | + |
| 105 | +```bash |
| 106 | +npm install |
| 107 | +``` |
| 108 | + |
| 109 | +Start the development server: |
| 110 | + |
| 111 | +```bash |
| 112 | +npm run dev |
| 113 | +``` |
| 114 | + |
| 115 | +The Next.js application will start on `http://localhost:3000`. |
| 116 | + |
| 117 | +### 4. Access the Application |
| 118 | + |
| 119 | +Open your browser and navigate to: |
| 120 | +- **Frontend:** http://localhost:3000 |
| 121 | +- **Backend API:** http://localhost:8000 |
| 122 | +- **API Documentation:** http://localhost:8000/docs |
| 123 | + |
| 124 | +## Features |
| 125 | + |
| 126 | +- **Browse Movies:** View a paginated list of movies from the sample_mflix dataset |
| 127 | +- **Search:** Full-text search using MongoDB Search |
| 128 | +- **Vector Search:** Semantic search using MongoDB Vector Search with Voyage AI embeddings |
| 129 | +- **Filter:** Filter movies by genre, year, rating, and more |
| 130 | +- **Movie Details:** View detailed information about each movie |
| 131 | +- **Aggregations:** Complex data aggregations and analytics |
| 132 | + |
| 133 | +## Development |
| 134 | + |
| 135 | +### Backend Development |
| 136 | + |
| 137 | +The Python FastAPI backend uses: |
| 138 | +- **FastAPI** for REST API framework |
| 139 | +- **PyMongo** for database operations |
| 140 | +- **Voyage AI** for vector embeddings |
| 141 | +- **fastapi** for ASGI server |
| 142 | + |
| 143 | +To run all tests: |
| 144 | + |
| 145 | +```bash |
| 146 | +cd server/ |
| 147 | +source .venv/bin/activate # or `.venv\Scripts\activate` on Windows |
| 148 | +pytest tests/ -v |
| 149 | +``` |
| 150 | + |
| 151 | +### Frontend Development |
| 152 | + |
| 153 | +The Next.js frontend uses: |
| 154 | +- **React 19** with TypeScript |
| 155 | +- **Next.js 16** with App Router |
| 156 | +- **Turbopack** for fast development builds |
| 157 | + |
| 158 | +#### Development Mode |
| 159 | + |
| 160 | +For active development with hot reloading and fast refresh: |
| 161 | + |
| 162 | +```bash |
| 163 | +cd client |
| 164 | +npm run dev |
| 165 | +``` |
| 166 | + |
| 167 | +This starts the development server on `http://localhost:3000` with Turbopack for fast rebuilds. |
| 168 | + |
| 169 | +#### Production Build |
| 170 | + |
| 171 | +To create an optimized production build and run it: |
| 172 | + |
| 173 | +```bash |
| 174 | +cd client |
| 175 | +npm run build # Creates optimized production build |
| 176 | +npm start # Starts production server |
| 177 | +``` |
| 178 | + |
| 179 | +The production build: |
| 180 | +- Minifies and optimizes JavaScript and CSS |
| 181 | +- Optimizes images and assets |
| 182 | +- Generates static pages where possible |
| 183 | +- Provides better performance for end users |
| 184 | + |
| 185 | +#### Linting |
| 186 | + |
| 187 | +To check code quality: |
| 188 | + |
| 189 | +```bash |
| 190 | +cd client |
| 191 | +npm run lint |
| 192 | +``` |
14 | 193 |
|
15 | 194 | ## Issues |
16 | 195 |
|
17 | 196 | If you have problems running the sample app, please check the following: |
18 | 197 |
|
19 | 198 | - [ ] Verify that you have set your MongoDB connection string in the `.env` file. |
| 199 | +- [ ] Verify that you have created and activated a Python `.venv` on Python v3.10 through v3.13. |
20 | 200 | - [ ] Verify that you have started the Python FastAPI server. |
21 | 201 | - [ ] Verify that you have started the Next.js client. |
22 | 202 | - [ ] Verify that you have no firewalls blocking access to the server or client ports. |
23 | 203 |
|
24 | 204 | If you have verified the above and still have issues, please |
25 | 205 | [open an issue](https://github.com/mongodb/docs-sample-apps/issues/new/choose) |
26 | 206 | on the source repository `mongodb/docs-sample-apps`. |
| 207 | + |
0 commit comments