ColinLefter / Accord

A real-time privacy-first social media platform leveraging feature-rich direct messaging text channels. Built as part of the course project for COSC 310 at UBC.
5 stars 1 forks source link

Configuring local connections to MongoDB Atlas #55

Open ColinLefter opened 6 months ago

ColinLefter commented 6 months ago

About

MongoDB Atlas is a multi-cloud database that can be accessed via your Atlas account as well as locally through MongoDB Compass or VS Code extension. You have 2 ways to interact with the database via a GUI:

  1. Through Atlas on your browser
  2. Through the Compass desktop application

In terms of writing queries, you will need to refer to the documentation for Query Documents. Watch this video for a quick intro on how everything works.

Setup Instructions

  1. Go to MongoDB Atlas and create an account
  2. Accept the invite to my project on Atlas
  3. In the Frontend/ directory, create a .env file and follow the instructions listed at the bottom of this doc
  4. Test your MongoDB connection to the frontend by having the development server for Next.js up (npm run dev) and then navigating to http://localhost:3000/api/testMongoDB. You should see the following if your connection is successful: {"message":"Successfully connected to MongoDB!"}
  5. Test your MongoDB connection to the backend by executing python manage.py under the backend directory. At the top of the list of available commands, you should see the following if your connection is successful: Pinged your deployment. You successfully connected to MongoDB!. Note: I configured Django so that the database connection is automatically established every time any Python file is executed.

.env file setup

Firstly, make sure you have the following installed: mongodb and dotenv. If not, do npm install mongodb and npm install dotenv or use yarn.

Inside the .env file, place the following content:

MONGODB_USERNAME=<username>
MONGODB_PASSWORD=<password>
MONGODB_URI=mongodb+srv://<username>:<password>@accord-systems.umbugbv.mongodb.net/?retryWrites=true&w=majority&appName=Accord-Systems

Set MONGODB_USERNAME and MONGODB_PASSWORD to the credentials for your Atlas account that I gave to you. Do not change replace <username> and <password> in MONGODB_URI. That will be replaced with a script that runs when the database connection is established in Next.js. For windows users, open PowerShell as administrator and execute New-Item -ItemType SymbolicLink -Path .\.env -Target ..\.env. This is needed because Django can access that .env file but Next.js can't as it is outside of the Next.js app (Frontend directory).

NOTE: do not include the angle brackets (<, >).

You do not need to worry about accidentally committing your .env file as it is already listed in .gitignore.