Database-MongoDB Setup
Create one working connection and reuse it across all three independent applications.
1. Create a project and cluster
- Open MongoDB Atlas and sign in or create an account.
- Create or select an Atlas project for these applications.
- Create a database deployment. Choose the free option if available for your account and region, or a deployment appropriate for your class. Provisioning and available tiers can vary.
- Select a nearby region and wait until the deployment is available. Loading sample data is optional and is not needed for these apps.
A cluster is the MongoDB deployment. A database such as todo_mvc exists within that deployment. You do not need a new cluster for each lab.
2. Create a database user
In the project’s database access/security area, create a database user using password authentication. Save its username and password privately. This is different from the account you use to log into the Atlas website.
Grant read/write access for the three example databases todo_mvc, todo_rest, and todo_personal, or use a dedicated database user with an appropriate read/write role for your lab. Index creation in the personal lab also needs the ability to create indexes. Avoid granting application users project-wide administration just to run CRUD.
Keep your database password private, including in screenshots. Use your own database user with the permissions required by these examples.
3. Add the machine’s network access
Open the project’s Network Access/IP access list and add your current public IP. A private address such as 192.168.x.x is not the public IP Atlas sees. If the console offers “Add Current IP Address,” use it. The running app must originate from an allowed address.
On a network with a shared NAT, several machines may share one public IP. VPNs and reconnecting networks can change it. Add the actual address used by the app. Use narrowly scoped network rules rather than opening access to every IP.
4. Copy the Drivers connection string
- Open Connect on the deployment.
- Choose Drivers and select Java.
- Copy the driver URI shown by Atlas. Use the host of your own cluster.
- Replace the username and password placeholders with the database user’s credentials.
- Keep the URI options supplied by Atlas; add authSource=admin if needed for the database user’s authentication database.
mongodb+srv://YOUR_DB_USER:YOUR_ENCODED_PASSWORD@YOUR_CLUSTER.mongodb.net/?retryWrites=true&w=majority&authSource=adminThe mongodb+srv scheme uses DNS SRV records to discover hosts and enables TLS by default. retryWrites asks the driver to retry supported writes; w=majority requests majority acknowledgement. authSource=admin names the authentication database, while the properties file names the data database.
Encode the username/password components, not the whole URI. Reserved characters need percent encoding: @ becomes %40, : becomes %3A, / becomes %2F, ? becomes %3F, # becomes %23, and % becomes %25. A password containing @, for example, cannot be pasted unescaped into the URI. Do not double-encode an already encoded password.
Atlas’s official walkthrough covers database users, network access, and Drivers connection strings.
5. Supply the connection securely
# Run in the SAME PowerShell terminal that will start Spring Boot.
$env:MONGODB_URI = 'mongodb+srv://YOUR_DB_USER:YOUR_ENCODED_PASSWORD@YOUR_CLUSTER.mongodb.net/?retryWrites=true&w=majority&authSource=admin'
# Replace the three placeholders with the Atlas Drivers connection details.
# Do not copy the example host literally.
mvn test
mvn spring-boot:runexport MONGODB_URI='mongodb+srv://YOUR_DB_USER:YOUR_ENCODED_PASSWORD@YOUR_CLUSTER.mongodb.net/?retryWrites=true&w=majority&authSource=admin'
mvn test
mvn spring-boot:runThe URI is read from the process environment. A PowerShell assignment applies only to that terminal and processes launched from it. Use single quotes to keep URI characters literal. Do not run the Maven commands until the selected project files exist.
Do not store a real URI in application.properties, a Postman collection, or a React VITE_* variable. The browser must never connect directly to Atlas or receive database credentials. Only the Java backend connects to MongoDB.
6. Verify actual persistence
Start the first application, add a todo, and refresh its list. In Atlas’s collection/data browser, inspect database todo_mvc and collection todos. A document should contain _id, title, completed, and possibly Spring’s _class mapping metadata. Stop and restart the application with the same configuration. The saved todo should still appear.
MongoDB creates databases and collections when documents are first written; they may not appear before you create a todo. The personal app can create users/personal_todos during index initialization. Its users collection must have a unique email index before accepting registrations.
Inspect the users indexes tab in Atlas and verify that email is unique. If index creation fails because duplicate sample records already exist, clean up the duplicate emails in that disposable example database and restart. Do not silently remove the unique constraint.
7. Diagnose connection problems
| Symptom | Cause and fix |
|---|---|
| mvn is not recognized | Install Maven and add its bin directory to PATH. Open a new terminal. Verify mvn -version. |
| Cannot resolve placeholder MONGODB_URI | Set the environment variable in the terminal that starts the app. IDE launches need an environment variable in their Run Configuration. Spring does not automatically read a .env file. |
| Connection points to localhost | Use spring.mongodb.uri for this Boot 4 project; a Boot 3 spring.data.mongodb.uri property is not the property used here. Check spelling and active configuration. |
| Authentication failed / code 18 | Use the Atlas database user, not your Atlas website login. Reset its password if needed. Percent-encode URI reserved characters. Verify authSource=admin. |
| Server selection timeout | Add your current public IP in Atlas Network Access; verify firewall access, active cluster, hostname, and outbound network connectivity. IP addresses can change on Wi-Fi or VPN. |
| UnknownHostException / SRV lookup failed | Re-copy the actual hostname. SRV requires DNS support; corporate DNS may block it. Use the standard connection string provided by Atlas if your environment cannot resolve SRV. |
| Address already in use | Stop the old process with Ctrl+C or override SERVER_PORT. If changing the personal API port, change React BASE and the Postman baseUrl too. |
| HTTP 400 | Check the JSON syntax, Content-Type, title, and completed. Inspect details in the JSON error. In the MVC lab, inspect the form validation message. |
| HTTP 404 | Check route and id. In the personal app, a task owned by another user also returns 404. |
| Browser fails but Postman works | Postman does not enforce browser CORS. Use the documented frontend origin exactly and inspect the browser Network panel. |
| Dependency download fails | Check proxy settings, Maven settings.xml, and repository reachability. An offline build works only after dependencies have already been downloaded. |
Ready to build
Have a supported JDK, Maven, your database user, your IP rule, and your own connection string ready.
Open Manual 01 →