- How to Integrate Vercel & MongoDB Step-by-Step :YouTube
- Getting Started with MongoDB Atlas - A Modern Database! :YouTube
- https://www.mongodb.com/docs/atlas/api/data-api/#when-to-use-the-data-api
- https://www.mongodb.com/docs/atlas/reference/partner-integrations/vercel/#std-label-vercel-access-lists
Start the development server, visit hte following URLs in the browser and watch the servers console output.
- http://localhost:3000/api/wrong
- http://localhost:3000/api/right
- http://localhost:3000/api/right-with-module
mkdir exc-nextjs-2023-with-mongodb-example
cd exc-nextjs-2023-with-mongodb-example
npx create-next-app@latest --example with-mongodb .
Watch the video linked above for more details...
npm -i vercel
Deploy to Vercel
npx vercel # --help
Pull the (already created) environment variables locally.
npx vercel env pull
This will generate .env.local
file.
- https://www.mongodb.com/docs/atlas/atlas-cli/
- https://www.mongodb.com/docs/atlas/cli/stable/install-atlas-cli/
wget -qO - https://pgp.mongodb.com/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt update
At this poin (20.06.2023) we need to convert the MongoDB's repository GPG key to to mitigate the apt
warning message...
APP="MongoDB"
LIST="/etc/apt/sources.list.d/mongodb-org-6.0.list"
KEY=$(sudo apt-key list 2>/dev/null | grep -i "$APP" -B1 | head -n1 | awk '{print $(NF-1)$NF}')
sudo apt-key export "$KEY" | sudo gpg --dearmour -o "/usr/share/keyrings/${APP}.gpg"
sudo sed -i.bak "s#]# signed-by=/usr/share/keyrings/${APP}.gpg]#" "$LIST"
sudo apt update
sudo apt-key del "$KEY"
sudo rm "${LIST}.bak"
sudo apt update
Invoke the following apt command to install both the Atlas CLI and mongosh:
sudo apt install mongodb-atlas
If you don't want to install mongosh
, invoke the following apt command instead to install the Atlas CLI only:
sudo apt install mongodb-atlas-cli
atlas setup
Follow the steps in the setup wizard to configure the Atlas CLI and the Browser. And finally connect to your Atlas project.
Alternatively you can use the atlas auth login
as it is described in the video tutorial [at 8:02]:
atlas auth login
atlas clusters loadSampleData Cluster0
-
You can load sample data via the Web UI as well.
-
Assuming
Cluster0
is the cluster you want to load sample data into. -
Note: Command
loadSampleData
is deprecated, useatlas clusters sampleData load
instead.
npm run dev
cd ~/Downloads
wget https://downloads.mongodb.com/compass/mongodb-compass_1.37.0_amd64.deb
sudo apt install ./mongodb-compass_1.37.0_amd64.deb
MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. This example will show you how to connect to and use MongoDB as your backend for your Next.js app.
If you want to learn more about MongoDB, visit the following pages:
Once you have access to the environment variables you'll need, deploy the example using Vercel:
Execute create-next-app
with npm, Yarn, or pnpm to bootstrap the example:
npx create-next-app --example with-mongodb with-mongodb-app
yarn create next-app --example with-mongodb with-mongodb-app
pnpm create next-app --example with-mongodb with-mongodb-app
Set up a MongoDB database either locally or with MongoDB Atlas for free.
Copy the env.local.example
file in this directory to .env.local
(which will be ignored by Git):
cp .env.local.example .env.local
Set each variable on .env.local
:
MONGODB_URI
- Your MongoDB connection string. If you are using MongoDB Atlas you can find this by clicking the "Connect" button for your cluster.
npm install
npm run dev
# or
yarn install
yarn dev
Your app should be up and running on http://localhost:3000! If it doesn't work, post on GitHub discussions.
You will either see a message stating "You are connected to MongoDB" or "You are NOT connected to MongoDB". Ensure that you have provided the correct MONGODB_URI
environment variable.
When you are successfully connected, you can refer to the MongoDB Node.js Driver docs for further instructions on how to query your database.
You can deploy this app to the cloud with Vercel (Documentation).
To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and import to Vercel.
Important: When you import your project on Vercel, make sure to click on Environment Variables and set them to match your .env.local
file.
Alternatively, you can deploy using our template by clicking on the Deploy button below.