Skip to content

Commit

Permalink
Express tute - mongodb setup screens (#35206)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishwillee authored Aug 1, 2024
1 parent a0386fc commit 76dcb4f
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ It will look something like this:

```js
const mongoDB =
"mongodb+srv://your_user_name:your_password@cluster0.lz91hw2.mongodb.net/local_library?retryWrites=true&w=majority";
"mongodb+srv://your_user_name:your_password@cluster0.cojoign.mongodb.net/local_library?retryWrites=true&w=majority";
```

Replace the line with the following code that uses `process.env.MONGODB_URI` to get the connection string from an environment variable named `MONGODB_URI` if has been set (use your own database URL instead of the placeholder below).
Expand All @@ -129,7 +129,7 @@ const mongoose = require("mongoose");
mongoose.set("strictQuery", false);

const dev_db_url =
"mongodb+srv://your_user_name:your_password@cluster0.lz91hw2.mongodb.net/local_library?retryWrites=true&w=majority";
"mongodb+srv://your_user_name:your_password@cluster0.cojoign.mongodb.net/local_library?retryWrites=true&w=majority";
const mongoDB = process.env.MONGODB_URI || dev_db_url;

main().catch((err) => console.log(err));
Expand Down
36 changes: 22 additions & 14 deletions files/en-us/learn/server-side/express_nodejs/mongoose/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,28 +589,37 @@ You will first need to [create an account](https://www.mongodb.com/cloud/atlas/r
After logging in, you'll be taken to the [home](https://cloud.mongodb.com/v2) screen:

1. Click the **+ Create** button in the _Overview_ section.

![Create a database on MongoDB Atlas.](mongodb_atlas_-_createdatabase.jpg)

2. This will open the _Deploy your database_ screen. Click on the **M0 FREE** option template.
2. This will open the _Deploy your cluster_ screen.
Click on the **M0 FREE** option template.

![Choose a deployment option when using MongoDB Atlas.](mongodb_atlas_-_deploy.jpg)

3. Scroll down the page to see the different options you can choose.
![Choose a cloud provider when using MongoDB Atlas.](mongodb_atlas_-_createsharedcluster.jpg)

- You can change the name of your Cluster under _Cluster Name_.
We are keeping it as `Cluster0` for this tutorial.
- Deselect the _Preload sample dataset_ checkbox, as we'll import our own sample data later on
- Select any provider and region from the _Provider_ and _Region_ sections. Different regions offer different providers.
- You can change the name of your Cluster under _Cluster Name_. We are naming it `Cluster0` for this tutorial.
- Tags are optional. We will not use them here.
- Click the **Create** button (creation of the cluster will take some minutes).
- Click the **Create deployment** button (creation of the cluster will take some minutes).

4. This will open the _Security Quickstart_ section.
![Set up the Access Rules on the Security Quickstart screen on MongoDB Atlas.](mongodb_atlas_-_securityquickstart.jpg)

- Enter a username and password. Remember to copy and store the credentials safely as we will need them later on. Click the **Create User** button.
- Enter a username and password for your application to use to access the database (above we have created a new login "cooluser").
Remember to copy and store the credentials safely as we will need them later on.
Click the **Create User** button.

> [!NOTE]
> Avoid using special characters in your MongoDB user password as mongoose may not parse the connection string properly.
- Enter `0.0.0.0/0` in the IP Address field. This tells MongoDB that we want to allow access from anywhere. Click the **Add Entry** button.
- Select **Add by current IP address** to allow access from your current computer
- Enter `0.0.0.0/0` in the IP Address field and then click the **Add Entry** button.
This tells MongoDB that we want to allow access from anywhere.

> [!NOTE]
> It is a best practice to limit the IP addresses that can connect to your database and other resources. Here we allow a connection from anywhere because we don't know where the request will come from after deployment.
Expand Down Expand Up @@ -640,25 +649,24 @@ After logging in, you'll be taken to the [home](https://cloud.mongodb.com/v2) sc
- Click the _Overview_ tab to return to the cluster overview.

10. From the Cluster0 _Overview_ screen click the **Connect** button.

![Configure connection after setting up a cluster in MongoDB Atlas.](mongodb_atlas_-_connectbutton.jpg)

11. This will open the _Connect to Cluster_ screen.
Click the **Drivers** option under the _Connect to your application_ section.
![Choose a connection type when connecting with MongoDB Atlas.](mongodb_atlas_-_chooseaconnectionmethod.jpg)
11. This will open the _Connect to Cluster0_ screen.

12. You will now be shown the _Connect_ screen.
![Choose the Short SRV connection when setting up a connection on MongoDB Atlas.](mongodb_atlas_-_connectforshortsrv.jpg)

- Select the Node driver and version as shown.
- **DO NOT** follow the step 2.
- Select your database user.
- Select the _Drivers_ category, then the _Driver_ **Node.js** and _Version_ as shown.
- **DO NOT** install the driver as suggested.
- Click the **Copy** icon to copy the connection string.
- Paste this in your local text editor.
- Update the username and password with your user's password.
- Replace `<password>` placeholder in the connection string with your user's password.
- Insert the database name "local_library" in the path before the options (`...mongodb.net/local_library?retryWrites...`)
- Save the file containing this string somewhere safe.

You have now created the database, and have a URL (with username and password) that can be used to access it.
This will look something like: `mongodb+srv://your_user_name:your_password@cluster0.lz91hw2.mongodb.net/local_library?retryWrites=true&w=majority`
This will look something like: `mongodb+srv://your_user_name:your_password@cluster0.cojoign.mongodb.net/local_library?retryWrites=true&w=majority&appName=Cluster0`

## Install Mongoose

Expand Down Expand Up @@ -826,7 +834,7 @@ The new things we show here are the field options:

Everything else should be familiar from our previous schema.

### Genre model - challenge!
### Genre model - challenge

Open your **./models/genre.js** file and create a schema for storing genres (the category of book, e.g. whether it is fiction or non-fiction, romance or military history, etc.).

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 76dcb4f

Please sign in to comment.