Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB sets my database to 'test' automatically. How to change it?

mongoose.connect(process.env.DATABASE_URL, {useNewUrlParser: true});
const MyModel = mongoose.model(mymodel, new Schema({ name: String }));

This creates a database named 'test' and a collection named 'mymodel'. How to change the 'test' name of my database?

like image 417
le huy Avatar asked Sep 06 '25 11:09

le huy


1 Answers

If you are posting data on remote mongoDB server, make sure you write Database name in connection url before "?retryWrites". There may be written as 'test' or nothing. If 'test' is there replace it with your database name, if not write database name just before '?' mark.

mongodb+srv://<username>:<password>@cluster0.vbaw8.mongodb.net/<database_name>?retryWrites=true&w=majority
like image 187
Vivek Gupta Avatar answered Sep 09 '25 04:09

Vivek Gupta