rsnext/examples/with-mongodb-mongoose/README.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
3.6 KiB
Markdown
Raw Permalink Normal View History

[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
# MongoDB and Mongoose with Next.js
This example shows how you can use a MongoDB database to support your Next.js application.
**Pet** is an application that allows users to add their pets' information (e.g., name, owner's name, diet, age, dislikes, likes, and photo). They can also delete it or edit it anytime.
## Deploy your own
Once you have access to [the environment variables you'll need](#step-2-set-up-environment-variables), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-mongodb-mongoose&project-name=with-mongodb-mongoose&repository-name=with-mongodb-mongoose&env=MONGODB_URI&envDescription=Required%20to%20connect%20the%20app%20with%20MongoDB&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-mongodb-mongoose%23step-2-set-up-environment-variables)
[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
```bash
npx create-next-app --example with-mongodb-mongoose with-mongodb-mongoose-app
```
```bash
[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
yarn create next-app --example with-mongodb-mongoose with-mongodb-mongoose-app
```
```bash
pnpm create next-app --example with-mongodb-mongoose with-mongodb-mongoose-app
[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
```
## Configuration
### Step 1. Get the connection string of your MongoDB server
In the case of MongoDB Atlas, it should be a string like this:
```
mongodb+srv://<username>:<password>@my-project-abc123.mongodb.net/test?retryWrites=true&w=majority
```
For more details, follow this [MongoDB Guide](https://docs.mongodb.com/guides/server/drivers/) on how to connect to MongoDB.
### Step 2. Set up environment variables
Copy the `.env.local.example` file in this directory to `.env.local` (which will be ignored by Git):
```bash
cp .env.local.example .env.local
```
Then set each variable on `.env.local`:
- `MONGODB_URI` should be the MongoDB connection string you got from step 1.
### Step 3. Run Next.js in development mode
```bash
npm install
npm run dev
# or
yarn install
yarn dev
```
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/vercel/next.js/discussions).
[ EXAMPLES ] Added with-mongodb example (#13028) * created with-mongodb, Next.js + MongoDB Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Michelle Lucero <31500626+MichelleLucero@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> * Update examples/with-mongodb/README.md Co-authored-by: Tim Neutkens <tim@timneutkens.nl> * modified for dotenv and environment var setup * removed extra instructions on readme, linked it to the MongoDB guides * changes to environment var * replaced document with react logic * removed Pet header * removed isomorphic-unfetch * replaced getInitialProps with getStaticProps (SSG) * removed console log * deleted next.config.js file * fixed prettier styling * fixed link 404 server rendering issue * modified server path for delete/edit * fixed prettier styling * fixed a typo * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * fixed typo, change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * change .env to .env.local Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-mongodb/package.json Co-authored-by: Joe Haddad <timer150@gmail.com> * removed dotenv dev dependency Co-authored-by: Joe Haddad <timer150@gmail.com> * cleaned up code and instructions * Update examples/with-mongodb/.gitignore Co-authored-by: Luis Alvarez D. <luis@vercel.com> * minor changes to code style and logic * Updated index page and .env.local.example * Updated new page * Don't use SSG for the edit page * Fixed revalidation issue * Use es6 for the model * Updated edit page * Removed VERCEL_URL * Updated readme and renamed env * Added temporal vercel.json * Removed seed until added with a script * Updated package.json * Renamed example in readme * Renamed example * Lint fix Co-authored-by: liulanz <zhengliulan@gmail.com> Co-authored-by: Shania Dhani <31454777+sdhani@users.noreply.github.com> Co-authored-by: Chi Shing Lee <60354073+chislee0708@users.noreply.github.com> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: sdhani <shania.dhani46@myhunter.cuny.edu> Co-authored-by: liulanz <37808313+liulanz@users.noreply.github.com> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Luis Alvarez D. <luis@vercel.com>
2020-05-30 19:15:00 +02:00
## Deploy on Vercel
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)).
#### Deploy Your Local Project
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).
**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file.
#### Deploy from Our Template
Alternatively, you can deploy using our template by clicking on the Deploy button below.
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-mongodb-mongoose&project-name=with-mongodb-mongoose&repository-name=with-mongodb-mongoose&env=MONGODB_URI&envDescription=Required%20to%20connect%20the%20app%20with%20MongoDB&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-mongodb-mongoose%23step-2-set-up-environment-variables)