Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a REST API With Express Modeling Data for the API Connecting Mongoose to MongoDB

Parvez Noor
PLUS
Parvez Noor
Courses Plus Student 15,917 Points

MongoDB course retired and no explanation of how to install or use it on this video

Hi guys,

I'm trying to follow this video but there seems to be a big jump from using express to using MongoDB. It would be fine, except installing mongodb doesn't seem very easy.

I've searched for the MongoDB course on here, but it is retired.

I've tried to use brew, and I've also set up a mongodb Atlas - however, setting these up with mongoose isn't straight forward.

I need some help and direction please.

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

I'd recommend these resources for your MongoDB educational journey.

Let me know if you have any questions.

Blake Larson
Blake Larson
13,014 Points

Here is how I am connected to Atlas and an export of the connectDB to be imported in your main express/node file.

                                                     // DB file
const mongoose = require("mongoose");


const db = ''; // Your MongoURI here

const connectDB = async () => {
  try {
    await mongoose.connect(db, {
      useNewUrlParser: true,
      useCreateIndex: true,
      useFindAndModify: false
    });

    console.log("Connected to the database");
  } catch (error) {
    console.log("This is the error");
    console.error(error.message);

    process.exit(1);
  }
};

module.exports = connectDB;
                                                      // Server file
const express = require("express");
const connectDB = require("./"); // Your DB file name

const app = express();

connectDB();

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log("SERVER RUNNING!");
});