Connect to MongoDB using Mongoose with Node.JS

I’ll show the method I use to connect to a mongo database, served local or from the cloud.

The database connection

This file isolates the database connection information, making it reusable and simplifying the rest of the application. I’ve included both local and cloud connection strings, which can be used interchangeably or simultaneously.

// FILE = ./config/database.js

// IMPORTANT: for security purposes, exclude this file from your git repo

module.exports = {
    'cloud': 'mongodb://admin:Password@database-url:port/database-name',
    'local': 'mongodb://127.0.0.1/database-name'
};Code language: JavaScript (javascript)

Using the connection

Use the connection in you main application file, app.js in my case. I’ve defined database.local to use the local database connection string, database.cloud would use the cloud database connection string.

// FILE = app.js

...

const database = require('./config/database.js');
mongoose.connect(database.local, { useNewUrlParser: true });

...Code language: PHP (php)

Additional considerations

Of course you will also need to install Mongoose and require it in your application.

// FILE = app.js

...

const mongoose = require('mongoose');

...Code language: PHP (php)

And if you want to use mongo locally, you’ll need to install it. This is highly recommended, as you’ll be able to keep working while offline. This is an easy tutorial for setting MongoDB up locally on a Mac.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.