- Published on
CS08 - Adding MongoDB
- Authors
- Name
- MDolce
Problems we need to solve:
Adding a MongoDB Atlas database using @vercel/postgres
Also, because of this error message:
Solution:
NOTE: Make sure to have a MongoDB Atlas account and database
- Import the project into a new GitHub repo
- Connect and deploy the project to an existing Vercel account
- Create a new
MongoDB Atlas
integration fromintegration tab
on dashboard in Vercel - If you are using Vercel CLI, verify that you have
vercel
installed - If not, run
npm install vercel
from terminal to add vercel to package.json - Run
vercel
from terminal then select a credential choice, to connectMongoDB Atlas
project to Vercel deployment - Run
vercel env pull .env.local
- Run `cp .env.example .env or create new .env project file at root level directory
- Copy the
MONGODB_URI
connection string to .envmongodb+srv://<USERNAME>:<PASSWORD>@cluster0.tdm0q.mongodb.net/<DBNAME>?retryWrites=true&w=majority
- Update .gitignore and add .env
Creating the endpoint with MongoDB
tracks-js
import clientPromise from '@/lib/mogodb'
export default async (req, res) => {
try {
const client = await clientPromise;
const db = client.db("ProjectIDX");
const getTracks = await db
.collection("tracks")
.find({})
.sort({ metacritic: -1 })
.limit(10)
.toArray();
res.json(movies);
} catch (e) {
console.error(e);
}
};
users-placeholder-data-js
const { db } = require('@vercel/postgres');
const {
users,