42

I'm trying to connect my app to mongodb, and I got a connection string from mongodb atlas, requiring me to replace username and password, which I get it, but it also says replace dbname with database name... I never remember I set up a db name, where do I find it? This is the connection string I have:

mongodb+srv://<username>:<password>@cluster0.pfose.mongodb.net/<dbname>?retryWrites=true&w=majority
1
  • 2
    You can use the default database name test. Once, you connect, you can see what other databases are there.
    – prasad_
    Aug 3, 2020 at 5:57

6 Answers 6

41

By default, the <dbname> is test but to create your own dbname, you need to change the <dbname> to the name of database you're interested to use. MongoDB Atlas will automatically create the db for you based on the name.
For example: Creating a db for Qlabs with username=Que and password=pin123

mongodb+srv://Que:[email protected]/Qlabs?retryWrites=true&w=majority
17

For those who wanna create db in with GUI

enter image description here

1
  • 1
    Yes, after loading example data, then under Collections tab I can see "Create Database" as you showed above.
    – Yang Wang
    Feb 11, 2021 at 3:25
6

If you have made an atlas account and went through the tutorial then they may have made you make a mock cluster, by default it's name will be Cluster0. That's what your will be.

2

**

By default the db name is <test> if you don't put anything in connection string also it will create a db, named as test.

**

But if you want to use your own specific db name, there is multiple method, I am specifing two only

Method 1: Speicy the db name directly in your connection string it will create a db for you

Ex of connection string: 'mongodb+srv://<username>:<password>@<cluster_name>.jqzujwt.mongodb.net/<dbname(optional)>?retryWrites=true&w=majority'

Method 2: From mongodb web UI (https://account.mongodb.com/account/login) you can create also

I am attaching the screenshoots for the better reference

After login using your credential it will land you on the below page

landing view of mongo web after login

Click on the browse collections buttom

It will redirct to below page

browse collection screen when there is no collection

from here you have to click on add my own data buttom

then the below popup will come you have to enter the Database name and Collection name and finally click on create butom

create db and collection popup

Final screenshoot after createing the db and collection

screenshoot after createing the db and collection

now if you want tocreate more db just click on + Create Database buttom and repeat the above step... booom...

1

Who have try the precedent answers and it won't work like me, you just have to include the --password field with the URL you have to past it in your shell,without missing to change the dbname to the default dbname 'test' exmple: mongo "mongodb+srv://cluster0.otnlg.mongodb.net/test" --username mourad54 --password txt147

0

Step 1: Click on Cluster0 (Cluster name)

enter image description here

It will bring you to the Cluster Overview page

Step 2: Click on Collections

enter image description here

It will show a list of databases. you can read name and create/delete databases from here.

Now lets interact with one of the database sample_mflix > collection movies

enter image description here

here is the sample code to Query/Search for a movie that has the title 'Back to the Future' from movies collection:

const { MongoClient } = require("mongodb");

// Replace the uri string with your MongoDB deployment's connection string.
const uri =
  "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority";

const client = new MongoClient(uri);

async function run() {
  try {
    await client.connect();

    const database = client.db('sample_mflix');
    const movies = database.collection('movies');

    // Query for a movie that has the title 'Back to the Future'
    const query = { title: 'Back to the Future' };
    const movie = await movies.findOne(query);

    console.log(movie);
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

ref: https://www.mongodb.com/docs/drivers/node/current/quick-start/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.