9951 explained code solutions for 126 technologies


expressjsHow do I connect an Express.js application to a MongoDB database?


Connecting an Express.js application to a MongoDB database is straightforward and can be done in just a few steps.

  1. Install and configure MongoDB and the Mongoose ORM package:
npm install mongoose
  1. Create a connection to the MongoDB database:
const mongoose = require('mongoose');
const MONGO_URL = 'mongodb://localhost:27017/myDatabase';
mongoose.connect(MONGO_URL, { useNewUrlParser: true });
  1. Create a Mongoose schema and model to represent the data in the database:
const userSchema = new mongoose.Schema({
  username: String,
  email: String
});
const User = mongoose.model('User', userSchema);
  1. Use the model to perform CRUD operations on the database:
const newUser = new User({
  username: 'John Doe',
  email: 'jdoe@example.com'
});

newUser.save(function(err) {
  if (err) {
    console.log(err);
  } else {
    console.log('User saved successfully!');
  }
});

Output example

User saved successfully!

  1. Finally, use the model in your Express.js routes to interact with the database:
app.get('/users', function(req, res) {
  User.find({}, function(err, users) {
    if (err) {
      res.send(err);
    } else {
      res.json(users);
    }
  });
});

These five steps will allow you to connect an Express.js application to a MongoDB database.

Helpful links

Edit this code on GitHub