Link Search Menu Expand Document

The best way to set the connection to a MongoDB database when using Spring is to set the value of the property spring.data.mongodb.uri, which is initialized in our code bases (e.g. proj-courses) from the environment variable MONGODB_URI.

Accordingly, you’ll see this in the .env.SAMPLE:

MONGODB_URI=see-instructions-in-readme

Sometimes you’ll be provided with a connection URI such as this one:

mongodb+srv://<username>:<password>@cluster0.a1bcd.mongodb.net/<dbname>?retryWrites=true&w=majority

It is important to note that <username>, <password> and <dbname> need to be replaced with actual values that you would be supplied with separately.

When you replace the values, do not include the <> characters.

Note that the <dbname> is typically just database (if you follow the instructions in the next step).

Creating the database

One person on the team will need to create the database. Here’s how that looks.

  • Navigate to Database
  • Click “Browse Collections”
  • Click “Add my own data”
  • Put in database as the name of the database and courses as the name of the collection

Now you have a database called “database”

mongodb-create-db

Get the value for the connection URI

This shows how to get the value for the connection string:

get-connection-string

Getting the value for password (from mongodb.com)

To get the value for password for a MongoDB database hosted on MongoDB.com,

  • Navigate to Database Access
  • Edit the user dbuser
  • Change the password (note that you cannot get the current password; you can only change the password)
  • Copy the password (and paste into your connection string in place of <password>
  • IMPORTANT: scroll down and save the changes otherwise the new password will not take effect!

get-mongo-password

You can also create a new user if you want to be sure that you will not impact other members of your group; in that case, you need to change dbuser in your connection string (e.g. to dbuserPhill)

Note that you need to give your user a name, and you also need to select a role (I forgot that the first time in the example shown, which is why the save doesn’t work the first time):

get-mongo-new-user

Configure for network access from anywhere

You may need to configure your MongoDb.com database for network access from anywhere.

Use this menu item:

image

Then, add the network address 0.0.0.0, which represents “the entire internet”.

add-all-internet