'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?



Solution 1:[1]

You have to change the connection string. The process.env.DATABASE_URL has to change. In you .env file, if you have it, change the DATABASE_URL to

mongodb://localhost:27017/myapp

Here the name of you db will be myapp. Now you can name it whatever you want instead of myapp. Just replace the text of myapp.

const MyModel = mongoose.model('Collection Name', new Schema({ name: String }));

Instead of mymodel give any choice of name for your collection in place of the string Collection Name. MongoDB saves the collections in lowercase letters even if you give uppercase collection name.

Solution 2:[2]

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

Solution 3:[3]

const MyModel = mongoose.model(mymodel, new Schema({ name: String }))

If you want to set Collection name you have to add third parameter. The third parameter is the Collection name. In your case:

const MyModel = mongoose.model('mymodel', new Schema({ name: String }), 'mymodel')

Solution 4:[4]

The problem is you have not specified the database name in your connection URI or neither in connection options:

const connctionUri = `mongodb://localhost:27017/database_name`
connect(connectionUri, connectionOptions).then(...).catch(...)

or this is the alternative for that,

const connectionpOptions = {
  dbName: `your database name`,
  useNewParser: true,
  useUnifiedTopology: true,
  .
  .
  .
}
const connectionUri = `mongodb://localhost:27017`
connect(connectionUri, connectionOptions).then(...).catch(...)

Solution 5:[5]

I came across the same issue while using Spring Boot in my macbook.

Looks like the default database name is 'test', So i changed the default DB in my case.

Before going ahead you cross check the default db name.

Step 1 - Execute below commands in terminal:

> mongo
> db

You can see test is the default DB.

Step 2 - Change DB name.

> Find .mongorc.js file
Since am using mac, i found this file under /Users/userName/.mongorc.js

Step 3 - Add below line of the code in .mongorc.js

db = db.getSiblingDB("yourDBname")

Step 4 - restart mongoDB and repeat step 1 so that you can see the default DB.

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 Sai Vamsi
Solution 2 Vivek Gupta
Solution 3 Paul Roub
Solution 4 Emad Baqeri
Solution 5 sujithramanathan