rsnext/examples/with-mongodb-mongoose
mustafaKamal-fe 4e27e00c30
Update dbConnect.js (#19644)
* Update dbConnect.js

The previous logic was in my opinion prone to errors in situations when different calls to this function from different locations may happen at the same time ( like a race condition  ). If the readyState is anything but not 0, then we must not try to perform a new connection as this may cause consequent connections!

I hope this makes sense

* Update examples/with-mongodb-mongoose/utils/dbConnect.js

* Lint fix and minor changes

Co-authored-by: Luis Alvarez D <luis@vercel.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2020-12-30 15:18:32 -05:00
..
components Fix: with-mongodb-mongoose example improper fetch error handling (#15856) 2020-08-05 07:37:29 +00:00
css [ EXAMPLES ] Added with-mongodb example (#13028) 2020-05-30 12:15:00 -05:00
models Fix typo in Pet mongoose model (#16943) 2020-09-08 17:00:25 +00:00
pages Fetch Data on Runtime (with-MongoDB-mongoose example) (#14674) 2020-06-29 16:45:24 -04:00
public [ EXAMPLES ] Added with-mongodb example (#13028) 2020-05-30 12:15:00 -05:00
utils Update dbConnect.js (#19644) 2020-12-30 15:18:32 -05:00
.env.local.example [ EXAMPLES ] Added with-mongodb example (#13028) 2020-05-30 12:15:00 -05:00
.gitignore Added .gitignore to examples that are deployed to vercel (#15127) 2020-07-16 10:52:23 -04:00
package.json Ensure all examples are MIT licensed (#16691) 2020-08-29 22:32:35 -04:00
README.md Simplify example usage instructions (#16678) 2020-08-29 22:22:02 -04:00
vercel.json [ EXAMPLES ] Added with-mongodb example (#13028) 2020-05-30 12:15:00 -05: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

Deploy the example using Vercel:

Deploy with Vercel

How to use

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example with-mongodb-mongoose with-mongodb-mongoose-app
# or
yarn create next-app --example with-mongodb-mongoose with-mongodb-mongoose-app

Install and run:

npm install
npm run dev
# or
yarn
yarn dev

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 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):

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

npm install
npm run dev

# or

yarn install
yarn dev

Deploy on Vercel

Deploy it to the cloud with Vercel (Documentation).