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

Issue

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

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.

Answered By – Sai Vamsi

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.