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
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:
Through Atlas on your browser
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.
In the Frontend/ directory, create a .env file and follow the instructions listed at the bottom of this doc
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!"}
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:
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.
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:
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
Frontend/
directory, create a .env file and follow the instructions listed at the bottom of this docnpm run dev
) and then navigating tohttp://localhost:3000/api/testMongoDB
. You should see the following if your connection is successful:{"message":"Successfully connected to MongoDB!"}
python manage.py
under thebackend
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
anddotenv
. If not, donpm install mongodb
andnpm install dotenv
or use yarn.Inside the .env file, place the following content:
Set
MONGODB_USERNAME
andMONGODB_PASSWORD
to the credentials for your Atlas account that I gave to you. Do not change replace<username>
and<password>
inMONGODB_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 executeNew-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.