'Where do I find my dbname for MongoDB connection string?
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
Solution 1:[1]
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
Solution 2:[2]
Solution 3:[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.
Solution 4:[4]
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
Solution 5:[5]
Step 1: Click on Cluster0 (Cluster name)
It will bring you to the Cluster Overview page
Step 2: Click on Collections
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
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/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Qudusayo |
Solution 2 | logbasex |
Solution 3 | Roohan |
Solution 4 | |
Solution 5 |