29

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
Jeffrey Lee
  • 304
  • 1
  • 3
  • 9
  • 2
    You can use the _default_ database name `test`. Once, you connect, you can see what other databases are there. – prasad_ Aug 03 '20 at 05:57

5 Answers5

25

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:pin123@cluster0.pfose.mongodb.net/Qlabs?retryWrites=true&w=majority
Qudusayo
  • 740
  • 5
  • 15
15

For those who wanna create db in with GUI

enter image description here

logbasex
  • 1,094
  • 1
  • 10
  • 15
  • 1
    Yes, after loading example data, then under Collections tab I can see "Create Database" as you showed above. – Yang Wang Feb 11 '21 at 03:25
3

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.

Roohan
  • 109
  • 1
  • 3
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

amine bob
  • 11
  • 2
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/

aakash4dev
  • 142
  • 1
  • 6