Skip to main content

MongoDB

By the end of this page you will have a MongoDB user, a database, and a connection from your lab.

1. Create a user

  1. Go to Services and click Manage on MongoDB.
  2. Click Add User (or Create User if you have none yet).
  3. Fill in Username, Password and Confirm Password, then confirm.
FieldRules
Username5–16 characters: letters, digits and underscores. Not only digits, and not a reserved name.
PasswordAt least 8 characters. Single quotes (') are not allowed.

2. Create a database

  1. Open the Database tab.
  2. Choose the Username that will own it.
  3. Type a Database Name. It is prefixed with the user's name and an underscore — notes owned by alice_dev becomes alice_dev_notes.
  4. Click Create Database.

Each user can own up to 5 databases.

3. Get the connection string

MongoDB users authenticate against a specific authentication database, and a connection that leaves it out is refused. The dashboard gives you a string with everything filled in:

  1. On the Dashboard tab, open the menu on the user's card.
  2. Choose Show Connection Info.
The Connection Info dialog showing Username, Password, Authentication Database and the Connection String with its copy button.

The dialog shows the Username, Password, Authentication Database and a ready-made Connection String. Copy the string.

4. Connect from your lab

Connect with the string you copied, using mongosh (install it in your lab from MongoDB's own instructions if it is not there):

mongosh "<connection-string>"

Then switch to your database:

use <user>_notes
db.items.insertOne({ title: "first" })
db.items.find()

From code, use the same connection string and name your database explicitly. Keep the authentication database parameter that is already in the string.

Forwarded to a different port?

If you are connecting through VS Code port forwarding from your own computer and it forwarded to a different local port, replace the host and port in the string with the forwarded ones.

Managing what you created

TaskWhere
Drop a databaseIts card on the Database tab
Delete a userDelete User in the card menu — this also deletes every database it owns

If it does not connect

SymptomCheck
Authentication failedThe authentication database is included; copy the string from Show Connection Info again
not authorized on …You are using a database this user owns, with its full prefixed name
Connection times outYou are connecting from inside a lab, or through the port forwarding helpers

Do it with an AI assistant

You can also do this by asking an AI assistant connected to your account — see Connect an AI assistant to set one up.

AskWhat the assistant does
“Create a MongoDB user called appsvc01”add_service_user
“Create a database called orders for it”create_database
“Give me the connection details for appsvc01”service_credentials
A new MongoDB user can do nothing yet

The user gets access only when a database is created for it — until then it connects and every read or write is refused. Ask the assistant to create the database in the same request.

Next