|
| 1 | +--- |
| 2 | +title: Using MongoDB with Hypermode |
| 3 | +sidebarTitle: MongoDB |
| 4 | +description: |
| 5 | + Connect your Hypermode agent to MongoDB for scalable document database |
| 6 | + operations |
| 7 | +--- |
| 8 | + |
| 9 | +<div className="flex items-center gap-3 mb-6"> |
| 10 | + <img |
| 11 | + src="/images/agents/connections/icons/mongodb.svg" |
| 12 | + alt="MongoDB" |
| 13 | + width={48} |
| 14 | + height={48} |
| 15 | + /> |
| 16 | + <div> |
| 17 | + <h2 className="text-2xl font-bold m-0">MongoDB</h2> |
| 18 | + <p className="text-gray-600 m-0"> |
| 19 | + Document-oriented NoSQL database platform |
| 20 | + </p> |
| 21 | + </div> |
| 22 | +</div> |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +MongoDB is a popular NoSQL document database that provides flexible schema |
| 27 | +design and powerful querying capabilities. This guide will walk you through |
| 28 | +connecting your Hypermode agent to MongoDB, enabling seamless document |
| 29 | +operations and data management for your applications. |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +Before connecting MongoDB to Hypermode, you'll need: |
| 34 | + |
| 35 | +1. A [MongoDB Atlas account](https://www.mongodb.com/cloud/atlas) or local |
| 36 | + MongoDB installation |
| 37 | +2. A MongoDB database with connection credentials |
| 38 | +3. A [Hypermode workspace](https://hypermode.com/) |
| 39 | + |
| 40 | +## Setting up MongoDB |
| 41 | + |
| 42 | +### Step 1: Create your MongoDB Atlas account |
| 43 | + |
| 44 | +If you haven't already, sign up for a |
| 45 | +[free MongoDB Atlas account](https://www.mongodb.com/cloud/atlas) to get started |
| 46 | +with cloud-hosted MongoDB. |
| 47 | + |
| 48 | +### Step 2: Create a cluster and database |
| 49 | + |
| 50 | +1. Create a new cluster in MongoDB Atlas |
| 51 | +2. Set up database access credentials |
| 52 | +3. Configure network access (whitelist IP addresses) |
| 53 | +4. Create your first database and collection |
| 54 | + |
| 55 | +Check the box to load sample data. This will create a movies database with |
| 56 | +sample data in several collections. |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +You'll also need to create a user and password for your database. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Step 3: Generate connection string |
| 65 | + |
| 66 | +Navigate to your cluster and get the connection string: |
| 67 | + |
| 68 | +1. Click "Connect" on your cluster |
| 69 | +2. Choose "Connect" |
| 70 | +3. Copy the connection string and replace `<password>` with your database user |
| 71 | + password. You'll use this connection string to connect your Hypermode agent |
| 72 | + to MongoDB. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +<Note> |
| 77 | + Your connection string will look like: |
| 78 | + `mongodb+srv://username:[email protected]/database?retryWrites=true&w=majority` |
| 79 | +</Note> |
| 80 | + |
| 81 | +### Step 4: Whitelist IP addresses |
| 82 | + |
| 83 | +Navigate to your cluster and whitelist IP addresses to allow connections from |
| 84 | +your Hypermode agent: |
| 85 | + |
| 86 | +1. Click "Network Access" on your cluster |
| 87 | +2. Add the IP address range `0.0.0.0/0` to allow connections from any IP address |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +## Creating your MongoDB agent |
| 92 | + |
| 93 | +### Step 1: Create a new agent |
| 94 | + |
| 95 | +From the Hypermode interface, create a new agent manually: |
| 96 | + |
| 97 | +1. Click the agent dropdown menu |
| 98 | +2. Select "Create new Agent" |
| 99 | + |
| 100 | +### Step 2: Configure agent settings |
| 101 | + |
| 102 | +Use these recommended settings for your MongoDB agent: |
| 103 | + |
| 104 | +- **Agent Name**: MongoAgent |
| 105 | +- **Agent Title**: Connects to MongoDB |
| 106 | +- **Description**: MongoAgent manages document operations |
| 107 | +- **Instructions**: You have a connection to MongoDB and various other developer |
| 108 | + tools to streamline document data access and management. You can perform CRUD |
| 109 | + operations, aggregations, and complex queries on MongoDB collections. |
| 110 | +- **Model**: GPT-4.1 |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +## Connecting to MongoDB |
| 115 | + |
| 116 | +### Step 1: Add the MongoDB connection |
| 117 | + |
| 118 | +Navigate to the **Connections** tab and add MongoDB: |
| 119 | + |
| 120 | +1. Click "Add connection" |
| 121 | +2. Select "MongoDB" from the dropdown |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +### Step 2: Configure credentials |
| 126 | + |
| 127 | +Enter your MongoDB credentials: |
| 128 | + |
| 129 | +- **Username**: The username of your MongoDB user |
| 130 | +- **Password**: The password of your MongoDB user |
| 131 | +- **Database Name**: The default database to connect to |
| 132 | +- **Hostname**: The hostname of your MongoDB cluster. This is the part of your |
| 133 | + MongoDB connection string that comes after `mongodb+srv://` and is a domain |
| 134 | + name. For example given the connection string |
| 135 | + `mongodb+srv://will:<db_password>@hypermodeturorials.o7ygcmn.mongodb.net/?retryWrites=true&w=majority&appName=HypermodeTurorials`, |
| 136 | + the hostname is `hypermodeturorials.o7ygcmn.mongodb.net` |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +<Warning> |
| 141 | + Keep your connection string secure! This contains your database credentials |
| 142 | + and should never be exposed in client-side code. |
| 143 | +</Warning> |
| 144 | + |
| 145 | +## Testing the connection |
| 146 | + |
| 147 | +Your agent can create collections, add documents, and perform queries on your |
| 148 | +MongoDB database. Since we created a sample movies database, let's test it out |
| 149 | +using the sample data in MongoDB. |
| 150 | + |
| 151 | +### Test 1: Query empty collections |
| 152 | + |
| 153 | +Start a new thread and test with a simple query: |
| 154 | + |
| 155 | +```text |
| 156 | +Can you show me all movies in the database? |
| 157 | +``` |
| 158 | + |
| 159 | +You should see a MongoDB tool call in the chat history, confirming the |
| 160 | +connection works: |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +### Test 2: Insert documents |
| 165 | + |
| 166 | +Now try adding data to your database: |
| 167 | + |
| 168 | +```text |
| 169 | +Can you add The Matrix from 1999 directed by the Wachowskis to my MongoDB database? |
| 170 | +``` |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +### Test 3: Complex queries |
| 175 | + |
| 176 | +Test more advanced operations: |
| 177 | + |
| 178 | +```text |
| 179 | +Can you find all movies from the 1910s and show their average rating? |
| 180 | +``` |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +## What you can do |
| 185 | + |
| 186 | +With your MongoDB connection established, your agent can: |
| 187 | + |
| 188 | +- **Query documents** with complex filters and projections |
| 189 | +- **Insert, update, and delete** documents |
| 190 | +- **Perform aggregations** for data analysis and reporting |
| 191 | +- **Work with embedded documents** and arrays |
| 192 | +- **Execute transactions** for multi-document operations |
| 193 | +- **Create indexes** for improved query performance |
| 194 | +- **Integrate with other tools** like GitHub, Slack, and Stripe |
| 195 | + |
| 196 | +## Best practices |
| 197 | + |
| 198 | +1. **Schema design**: Design your document structure to match your query |
| 199 | + patterns |
| 200 | +2. **Indexing**: Create indexes on frequently queried fields |
| 201 | +3. **Connection management**: Use connection pooling for better performance |
| 202 | +4. **Error handling**: Your agent will handle common database errors gracefully |
| 203 | +5. **Data validation**: Consider using MongoDB schema validation for data |
| 204 | + consistency |
| 205 | + |
| 206 | +## Advanced operations |
| 207 | + |
| 208 | +### Aggregation pipelines |
| 209 | + |
| 210 | +Your agent can perform complex aggregation operations: |
| 211 | + |
| 212 | +```text |
| 213 | +Can you group movies by decade and show the count and average rating for each decade? |
| 214 | +``` |
| 215 | + |
| 216 | +### Text search |
| 217 | + |
| 218 | +Enable text search on your collections: |
| 219 | + |
| 220 | +```text |
| 221 | +Can you find all movies that mention "robot" in their title or description? |
| 222 | +``` |
| 223 | + |
| 224 | +### Geospatial queries |
| 225 | + |
| 226 | +For location-based data: |
| 227 | + |
| 228 | +```text |
| 229 | +Find all movie theaters within 10 miles of coordinates [40.7128, -74.0060] |
| 230 | +``` |
| 231 | + |
| 232 | +## Troubleshooting |
| 233 | + |
| 234 | +### Common connection issues |
| 235 | + |
| 236 | +1. **Network access**: Ensure your IP is whitelisted in MongoDB Atlas |
| 237 | +2. **Authentication**: Verify your username and password are correct |
| 238 | +3. **Connection string**: Check that your connection string format is valid |
| 239 | +4. **Database permissions**: Ensure your user has appropriate read/write |
| 240 | + permissions |
| 241 | + |
| 242 | +### Performance optimization |
| 243 | + |
| 244 | +1. **Query optimization**: Use explain() to analyze query performance |
| 245 | +2. **Index usage**: Monitor index usage and create appropriate indexes |
| 246 | +3. **Document size**: Keep documents reasonably sized for better performance |
| 247 | +4. **Connection pooling**: Configure appropriate connection pool settings |
| 248 | + |
| 249 | +## Learn more |
| 250 | + |
| 251 | +- [MongoDB Documentation](https://docs.mongodb.com/) |
| 252 | +- [MongoDB Atlas Documentation](https://docs.atlas.mongodb.com/) |
| 253 | +- [MongoDB Query Language](https://docs.mongodb.com/manual/tutorial/query-documents/) |
| 254 | +- [Aggregation Framework](https://docs.mongodb.com/manual/aggregation/) |
| 255 | + |
| 256 | +<Tip> |
| 257 | + Combine MongoDB with other Hypermode connections to build powerful workflows. |
| 258 | + For example, use GitHub to track code changes that affect your data models, or |
| 259 | + Slack to notify your team of important database updates and analytics |
| 260 | + insights. |
| 261 | +</Tip> |
| 262 | + |
| 263 | +## Example workflows |
| 264 | + |
| 265 | +### E-commerce integration |
| 266 | + |
| 267 | +```text |
| 268 | +Track inventory levels and automatically update product availability when stock changes |
| 269 | +``` |
| 270 | + |
| 271 | +### Content management |
| 272 | + |
| 273 | +```text |
| 274 | +Manage blog posts, user comments, and media assets with flexible document structures |
| 275 | +``` |
| 276 | + |
| 277 | +### Analytics and reporting |
| 278 | + |
| 279 | +```text |
| 280 | +Generate real-time reports on user behavior, sales metrics, and application performance |
| 281 | +``` |
0 commit comments