Skip to content

Commit 0152dac

Browse files
kukicadolfadeskodiakhq[bot]
authored
MongoDB Example (vercel#15029)
* MongoDB Example * Apply suggestions from code review * Add changes based on feedback. * clean up code with more descriptive props * Use MongoDB in ServerSideProps instead of separate API route * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> * Update examples/with-mongodb/README.md Co-authored-by: Luis Alvarez D. <[email protected]> Co-authored-by: Luis Alvarez D <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 53a3ffe commit 0152dac

File tree

8 files changed

+401
-0
lines changed

8 files changed

+401
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MONGODB_URI=
2+
MONGODB_DB=

examples/with-mongodb/.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
21+
# debug
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*
25+
26+
# local env files
27+
.env.local
28+
.env.development.local
29+
.env.test.local
30+
.env.production.local

examples/with-mongodb/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## Example app using MongoDB
2+
3+
[MongoDB](https://www.mongodb.com/) 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.
4+
5+
If you want to learn more about MongoDB, visit the following pages:
6+
7+
- [MongoDB Atlas](https://mongodb.com/atlas)
8+
- [MongoDB Documentation](https://docs.mongodb.com/)
9+
10+
## Deploy your own
11+
12+
Once you have access to the environment variables you'll need, deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
13+
14+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-mongodb&env=MONGODB_URI,MONGODB_DB&envDescription=Required%20to%20connect%20the%20app%20with%20MongoDB)
15+
16+
## How to use
17+
18+
### Using `create-next-app`
19+
20+
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
21+
22+
```bash
23+
npx create-next-app --example with-mongodb with-mongodb
24+
# or
25+
yarn create next-app --example with-mongodb with-mongodb
26+
```
27+
28+
## Configuration
29+
30+
### Set up a MongoDB database
31+
32+
Set up a MongoDB database either locally or with [MongoDB Atlas for free](https://mongodb.com/atlas).
33+
34+
### Set up environment variables
35+
36+
Copy the `env.local.example` file in this directory to `.env.local` (which will be ignored by Git):
37+
38+
```bash
39+
cp .env.local.example .env.local
40+
```
41+
42+
Set each variable on `.env.local`:
43+
44+
- `MONGODB_URI` - Your MongoDB connection string. If you are using [MongoDB Atlas](https://mongodb.com/atlas) you can find this by clicking the "Connect" button for your cluster.
45+
- `MONGODB_DB` - The name of the MongoDB database you want to use.
46+
47+
### Run Next.js in development mode
48+
49+
```bash
50+
npm install
51+
npm run dev
52+
53+
# or
54+
55+
yarn install
56+
yarn dev
57+
```
58+
59+
Your app should be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, post on [GitHub discussions](https://github.com/zeit/next.js/discussions).
60+
61+
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` and `MONGODB_DB` environment variables.
62+
63+
When you are successfully connected, you can refer to the [MongoDB Node.js Driver docs](https://mongodb.github.io/node-mongodb-native/3.4/tutorials/collections/) for further instructions on how to query your database.
64+
65+
## Deploy on Vercel
66+
67+
You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
68+
69+
#### Deploy Your Local Project
70+
71+
To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/import/git?utm_source=github&utm_medium=readme&utm_campaign=next-example).
72+
73+
**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file.
74+
75+
#### Deploy from Our Template
76+
77+
Alternatively, you can deploy using our template by clicking on the Deploy button below.
78+
79+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-mongodb&env=MONGODB_URI,MONGODB_DB&envDescription=Required%20to%20connect%20the%20app%20with%20MongoDB)

examples/with-mongodb/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "with-mongodb",
3+
"version": "0.1.0",
4+
"scripts": {
5+
"dev": "next dev",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"mongodb": "^3.5.9",
11+
"next": "latest",
12+
"react": "^16.13.1",
13+
"react-dom": "^16.13.1"
14+
},
15+
"license": "MIT"
16+
}

examples/with-mongodb/pages/index.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import Head from 'next/head'
2+
import { connectToDatabase } from '../util/mongodb'
3+
4+
export default function Home({ isConnected }) {
5+
return (
6+
<div className="container">
7+
<Head>
8+
<title>Create Next App</title>
9+
<link rel="icon" href="/favicon.ico" />
10+
</Head>
11+
12+
<main>
13+
<h1 className="title">
14+
Welcome to <a href="https://nextjs.org">Next.js with MongoDB!</a>
15+
</h1>
16+
17+
{isConnected ? (
18+
<h2 className="subtitle">You are connected to MongoDB</h2>
19+
) : (
20+
<h2 className="subtitle">
21+
You are NOT connected to MongoDB. Check the <code>README.md</code>{' '}
22+
for instructions.
23+
</h2>
24+
)}
25+
26+
<p className="description">
27+
Get started by editing <code>pages/index.js</code>
28+
</p>
29+
30+
<div className="grid">
31+
<a href="https://nextjs.org/docs" className="card">
32+
<h3>Documentation &rarr;</h3>
33+
<p>Find in-depth information about Next.js features and API.</p>
34+
</a>
35+
36+
<a href="https://nextjs.org/learn" className="card">
37+
<h3>Learn &rarr;</h3>
38+
<p>Learn about Next.js in an interactive course with quizzes!</p>
39+
</a>
40+
41+
<a
42+
href="https://github.com/vercel/next.js/tree/master/examples"
43+
className="card"
44+
>
45+
<h3>Examples &rarr;</h3>
46+
<p>Discover and deploy boilerplate example Next.js projects.</p>
47+
</a>
48+
49+
<a
50+
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
51+
className="card"
52+
>
53+
<h3>Deploy &rarr;</h3>
54+
<p>
55+
Instantly deploy your Next.js site to a public URL with Vercel.
56+
</p>
57+
</a>
58+
</div>
59+
</main>
60+
61+
<footer>
62+
<a
63+
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
64+
target="_blank"
65+
rel="noopener noreferrer"
66+
>
67+
Powered by{' '}
68+
<img src="/vercel.svg" alt="Vercel Logo" className="logo" />
69+
</a>
70+
</footer>
71+
72+
<style jsx>{`
73+
.container {
74+
min-height: 100vh;
75+
padding: 0 0.5rem;
76+
display: flex;
77+
flex-direction: column;
78+
justify-content: center;
79+
align-items: center;
80+
}
81+
82+
main {
83+
padding: 5rem 0;
84+
flex: 1;
85+
display: flex;
86+
flex-direction: column;
87+
justify-content: center;
88+
align-items: center;
89+
}
90+
91+
footer {
92+
width: 100%;
93+
height: 100px;
94+
border-top: 1px solid #eaeaea;
95+
display: flex;
96+
justify-content: center;
97+
align-items: center;
98+
}
99+
100+
footer img {
101+
margin-left: 0.5rem;
102+
}
103+
104+
footer a {
105+
display: flex;
106+
justify-content: center;
107+
align-items: center;
108+
}
109+
110+
a {
111+
color: inherit;
112+
text-decoration: none;
113+
}
114+
115+
.title a {
116+
color: #0070f3;
117+
text-decoration: none;
118+
}
119+
120+
.title a:hover,
121+
.title a:focus,
122+
.title a:active {
123+
text-decoration: underline;
124+
}
125+
126+
.title {
127+
margin: 0;
128+
line-height: 1.15;
129+
font-size: 4rem;
130+
}
131+
132+
.title,
133+
.description {
134+
text-align: center;
135+
}
136+
137+
.subtitle {
138+
font-size: 2rem;
139+
}
140+
141+
.description {
142+
line-height: 1.5;
143+
font-size: 1.5rem;
144+
}
145+
146+
code {
147+
background: #fafafa;
148+
border-radius: 5px;
149+
padding: 0.75rem;
150+
font-size: 1.1rem;
151+
font-family: Menlo, Monaco, Lucida Console, Liberation Mono,
152+
DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace;
153+
}
154+
155+
.grid {
156+
display: flex;
157+
align-items: center;
158+
justify-content: center;
159+
flex-wrap: wrap;
160+
161+
max-width: 800px;
162+
margin-top: 3rem;
163+
}
164+
165+
.card {
166+
margin: 1rem;
167+
flex-basis: 45%;
168+
padding: 1.5rem;
169+
text-align: left;
170+
color: inherit;
171+
text-decoration: none;
172+
border: 1px solid #eaeaea;
173+
border-radius: 10px;
174+
transition: color 0.15s ease, border-color 0.15s ease;
175+
}
176+
177+
.card:hover,
178+
.card:focus,
179+
.card:active {
180+
color: #0070f3;
181+
border-color: #0070f3;
182+
}
183+
184+
.card h3 {
185+
margin: 0 0 1rem 0;
186+
font-size: 1.5rem;
187+
}
188+
189+
.card p {
190+
margin: 0;
191+
font-size: 1.25rem;
192+
line-height: 1.5;
193+
}
194+
195+
.logo {
196+
height: 1em;
197+
}
198+
199+
@media (max-width: 600px) {
200+
.grid {
201+
width: 100%;
202+
flex-direction: column;
203+
}
204+
}
205+
`}</style>
206+
207+
<style jsx global>{`
208+
html,
209+
body {
210+
padding: 0;
211+
margin: 0;
212+
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
213+
Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue,
214+
sans-serif;
215+
}
216+
217+
* {
218+
box-sizing: border-box;
219+
}
220+
`}</style>
221+
</div>
222+
)
223+
}
224+
225+
export async function getServerSideProps(context) {
226+
const { client } = await connectToDatabase()
227+
228+
const isConnected = await client.isConnected() // Returns true or false
229+
230+
return {
231+
props: { isConnected },
232+
}
233+
}
14.7 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)