What is a MongoDB URI?

What is a MongoDB URI?

The MongoDB URI is a way to connect to a MongoDB database. To get a MongoDB URI, you need to create a MongoDB database on your computer or use an online MongoDB service. Here's how you can get a MongoDB URI:

MongoDB Atlas (Cloud-based MongoDB):

  1. Create an account on MongoDB Atlas:

    • Go to MongoDB Atlas.

    • Sign up for a new account if you don't have one.

  2. Create a new cluster:

    • Log in to MongoDB Atlas.

    • Click on the "Clusters" tab, and then click the "Build a Cluster" button.

    • Follow the steps to configure your cluster (choose cloud provider, region, etc.).

    • Click the "Create Cluster" button.

  3. Create a MongoDB user:

    • In the left sidebar, click on "Database Access."

    • Click the "Add New Database User" button and create a new user with a username and password.

  4. Get the connection string (MongoDB URI):

    • In the left sidebar, click on "Clusters."

    • Click the "Connect" button for your cluster.

    • Choose "Connect Your Application."

    • Copy the connection string. It will look something like this:

        mongodb+srv://<username>:<password>@cluster0.mongodb.net/<database>
      
    • Replace <username>, <password>, and <database> with your actual MongoDB Atlas username, password, and database name.

Local MongoDB Installation:

If you have MongoDB set up on your computer, you can use a local connection string. Here's an easy example:

mongodb://localhost:27017/yourlocaldbname

Replace yourlocaldbname with the name you want to give to your local database.

Update server.js:

Replace the MongoDB URI in server.js with the one you obtained:

// Connect to MongoDB (replace 'your-mongodb-uri' with your actual MongoDB connection string)
mongoose.connect('mongodb://localhost:27017/yourlocaldbname', { useNewUrlParser: true, useUnifiedTopology: true });

Or for MongoDB Atlas:

// Connect to MongoDB (replace 'your-mongodb-uri' with your actual MongoDB connection string)
mongoose.connect('mongodb+srv://<username>:<password>@cluster0.mongodb.net/<database>', { useNewUrlParser: true, useUnifiedTopology: true });

Replace <username>, <password>, and <database> with your MongoDB Atlas username, password, and database name.

After updating the MongoDB URI, restart your server (node server.js) and run your React app (npm start) to see the changes.