Skip to main content

MongoDB

This page shows you how to create a MongoDB user and a database, and how to connect to them from your lab.

1. Create a user​

  1. Go to Services and select Manage on MongoDB.
  2. Select 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. For example, notes owned by alice_dev becomes alice_dev_notes.
  4. Select Create Database.

Each user card shows how many databases the user owns and how many it can own. At the limit, you see Database limit exceeded for the user.

3. Get the connection string​

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

  1. On the Dashboard tab, open the menu on the user's card.
  2. Choose Show Connection Info.
Shown on labs.selfmade.ninja, the Selfmade Ninja Academy platform. On your own platform you will see your own domain and data.

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. If mongosh is not in your lab, install it by following MongoDB's own instructions.

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 give your database name explicitly. Keep the authentication database parameter that is already in the string.

Forwarded to a different port?

If you connect from your own computer through VS Code port forwarding, and VS Code 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 can connect, but every read or write is refused. Ask the assistant to create the database in the same request.

Next​