Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed User Authentication With Express and Mongo!
You have completed User Authentication With Express and Mongo!
Preview
In this video you'll learn how to install Mongoose and create a connection to a MongoDB database.
New Terms
- ODM — Object Data Modelling
Treehouse Courses and Videos
Instructions
Guides and references
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've created a registration form for
0:00
the bookworm site, but
that's just the beginning.
0:02
Although we can submit information
from the form, we don't yet
0:06
have a way to save that data.
0:09
That's where a database comes in.
0:11
In this course we'll use MongoDB
to store user registration data.
0:13
And to make working with Mongo easier,
we'll use a node module called Mongoose.
0:17
Mongoose is what's known as an ODM or
object data modeling library.
0:22
An ODM simplifies the process of adding,
searching,
0:28
and updating records in
a document-based database.
0:31
Mongoose is the most popular ODM for
working with Express and MongoDB.
0:34
So it's a great choice for this project.
0:38
Let's get started by installing Mongoose.
0:40
If you're following along, you'll need to
have MongoDB installed on your computer.
0:44
I'm not going to take you
through that step here.
0:48
I've listed instructions in
the teacher's notes as well as a link to
0:51
other treehouse courses
that teach Mongo DB.
0:54
In addition, if you're following along and
you've skipped a video or
0:56
your code doesn't seem to be working, you
can catch up with me by using the console
1:00
or terminal to navigate to the proper
folder in the downloaded files.
1:05
For example, S2V3 for this video, and
1:10
typing npm install to get
the node modules in place.
1:13
You can then open that folder in your
text editor, and follow along from there.
1:19
It's time to install mongoose.
1:26
Open a terminal program and
navigate to the directory for
1:29
this project and
type npm install --save mongoose.
1:33
Remember that the --save flag adds this
dependency to the package.json file.
1:38
This makes it easy for other people
to install the entire application
1:44
with a simple npm install command,
and it means we don't have to package
1:48
multiple megabytes of node modules to
share this project with someone else.
1:52
To add Mongoose to our application and
connect to MongoDB,
1:58
we'll add some code to
the app.js file in our project.
2:01
First we need to require Mongoose.
2:05
I'll add it up here where
I load other node modules.
2:07
I'm storing this in
a variable called mongoose.
2:13
We can then use that variable whenever
we want to use Mongoose to connect
2:17
the Mongo DB.
2:21
Next I'll connect to
Mongo DB using Mongoose.
2:22
We're passing one argument to
the connect method, the Mongo database.
2:28
This connects to Mongo DB on this machine,
2:33
local host on port 27017.
2:36
That's the default port for Mongo DB.
2:42
The last part here is the name of
the Mongo data store, bookworm,
2:46
the name of the database for our site.
2:51
That database doesn't yet exist, but when
our app starts, Mongoose will create it.
2:53
Now, let's create a variable to hold
the database connection object.
2:58
I'll call it db.
3:04
And it represents that
connection to MongoDB.
3:08
We can use that object
to add an error handler.
3:11
This code listens for error events or
errors that occur with our database
3:15
connection, and
logs those errors to the console.
3:19
Let's see how it works.
3:22
I'll save this file, and
return to my terminal program.
3:26
And I'll start it up by typing nodemon.
3:30
It looks like we have an error.
3:37
It says connection error.
3:39
Yeah, I haven't started Nongo yet.
3:40
I can do that on my Mac just
by running the Mongo daemon.
3:43
I'll open another tab and
type mongod and hit return.
3:46
That starts up Mongo.
3:52
If you're on Windows, you need to find and
start the Mongo executable file.
3:54
See the teacher's notes below.
3:58
Once Mongo is running,
I can start my app again.
4:00
Notice that the server is still running
despite the lack of database connection,
4:04
and nodemon unfortunately doesn't restart
automatically when Mongo is started.
4:08
So I need to stop the express
server with Ctrl+C.
4:13
Then I can start it again with nodemon.
4:18
Great!
4:22
This time no errors, so the application
has successfully connected to Mongo.
4:22
In the next video, we'll create a model,
which is also called a schema,
4:27
for the registration form
data we want to collect.
4:30
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up