Node.js Essential Training
Illustration by Richard Downs

Connecting to MongoDB via Mongoose


From:

Node.js Essential Training

with Joseph LeBlanc

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Connecting to MongoDB via Mongoose

MongoDB is a document oriented database using Javascript as its query language. Modules provided by the node community, make it easy to connect to a MongoDB database. In this video, we'll install one of those modules and connect to MongoDB. First let's go to the exercise files and pull out the project. Go to chapter seven, video one and then copy the start folder to your desktop. Next, go to Terminal and then change directory to that folder. I'm just going to drag the folder in to get the path.
Expand all | Collapse all
  1. 3m 9s
    1. Welcome
      42s
    2. What you should know before watching this course
      57s
    3. Using the exercise files
      47s
    4. Using the challenges
      43s
  2. 18m 50s
    1. Why use Node.js?
      2m 1s
    2. Choosing a Node.js installation process
      4m 56s
    3. Installing Node.js on the Mac
      2m 5s
    4. Installing Node.js on Windows
      1m 43s
    5. Installing Node.js via Node Version Manager on Ubuntu Linux
      4m 45s
    6. Installing Node.js via Node Version Manager on a Mac
      3m 20s
  3. 13m 54s
    1. Exploring language additions to the V8 JavaScript engine
      3m 38s
    2. Understanding require() and modules
      6m 39s
    3. Understanding callback execution in the event loop
      3m 37s
  4. 12m 23s
    1. Initializing Node.js projects
      2m 37s
    2. Finding an NPM
      3m 30s
    3. Maintaining projects using the npm command
      6m 16s
  5. 21m 4s
    1. Creating modules with getters and setters
      6m 33s
    2. Understanding module caching and scopes
      6m 15s
    3. Implementing JavaScript creational patterns
      5m 8s
    4. Challenge: Leveraging module caching
      55s
    5. Solution: Leveraging module caching
      2m 13s
  6. 32m 27s
    1. Starting Express applications
      3m 42s
    2. Handling GET requests returning JSON
      3m 26s
    3. Navigating large datasets using request variables in routes
      5m 4s
    4. Modifying data through PUT requests
      4m 32s
    5. Supplying middleware to modify HTTP response headers
      4m 37s
    6. Generating HTML views in Jade
      8m 9s
    7. Challenge: List all records as JSON
      37s
    8. Solution: List all records as JSON
      2m 20s
  7. 25m 37s
    1. Evaluating unit testing methodologies
      3m 8s
    2. Isolating existing code for testing
      6m 47s
    3. Supplying helpers for test data
      2m 35s
    4. Writing unit tests with Mocha
      6m 5s
    5. Simulating HTTP requests with SuperTest
      7m 2s
  8. 28m 45s
    1. Connecting to MongoDB via Mongoose
      4m 16s
    2. Defining schemas for collections in Mongoose
      3m 47s
    3. Querying MongoDB collections
      5m 32s
    4. Persisting Express sessions through MongoDB and mongo-connect
      7m 6s
    5. Logging in to Express applications using Passport
      8m 4s
  9. 21m 47s
    1. Controlling the built-in REPL
      4m 18s
    2. Constructing a custom REPL
      5m 40s
    3. Receiving command-line arguments with Optimist
      6m 14s
    4. Building command-line tools
      3m 35s
    5. Challenge: Launch a custom REPL from a command
      41s
    6. Solution: Launch a custom REPL from a command
      1m 19s
  10. 15m 46s
    1. Emitting events and attaching listeners
      5m 32s
    2. Streaming chunked data through readable streams
      2m 33s
    3. Controlling readable streams
      1m 52s
    4. Piping readable data into writable streams
      2m 10s
    5. Handling duplex streams over TCP
      3m 39s
  11. 4m 6s
    1. Deploying projects to cloud hosting services
      2m 36s
    2. Finding Node.js resources
      1m 30s

please wait ...
Watch the Online Video Course Node.js Essential Training
3h 17m Intermediate Oct 16, 2013

Viewers: in countries Watching now:

Node.js is a powerful tool for controlling web servers, building applications, and creating event-driven programming. And it brings JavaScript—a language familiar to all web developers—into an environment independent of web browsers. Learn all about Node.js and start creating JavaScript applications in this course with Joseph LeBlanc. He shows how to install Node.js on Mac, Windows, and Linux and dives deep into its modules and Express framework for app development. Learn how to unit test your code, start sessions on web servers, stream data, and create simple command-line tools.

Topics include:
  • Why use Node.js?
  • Installing Node.js
  • Understanding the event loop
  • Initializing Node.js projects
  • Creating modules with getters and setters
  • Starting Express applications
  • Testing your code
  • Working with sessions and databases
  • Building command-line tools
  • Emitting events and attaching listeners
  • Controlling readable streams
Subject:
Developer
Software:
JavaScript Node.js
Author:
Joseph LeBlanc

Connecting to MongoDB via Mongoose

MongoDB is a document oriented database using Javascript as its query language. Modules provided by the node community, make it easy to connect to a MongoDB database. In this video, we'll install one of those modules and connect to MongoDB. First let's go to the exercise files and pull out the project. Go to chapter seven, video one and then copy the start folder to your desktop. Next, go to Terminal and then change directory to that folder. I'm just going to drag the folder in to get the path.

And now type npm install and then press Return. So now our project has all the modules it needs and is ready to go. Before connecting to MongoDB, you need to have a MongoDB database warmed up and ready to go. There are many options for doing this, but two I would like to point out. The first one is to install MongoDB on your computer, to do this, go to MongoDB.org and head to the downloads page. One thing to note though is that this doesn't come as a dot PKG file with a wizard based installer, instead you get some binary files which you then have to place in one of your executable paths.

The other option is to use a service like MongoLab. MongoLab will host a MongoDB database for you. Once you've signed up for MongoLab and created a database it will provide you with a connection string to use in your application. The downside is that the speed of data transfer will be slower than what it would be if you had a local MongoDB installation. I'll be using MongoLab here so I don't have to install MongoDB locally. I already have a MongoLab account, so I'm going to log in.

So now that I've logged into MongoLab, I can create a database. I'm going do this by clicking the create new button in the databases section. From here, you can select a cloud provider. I'm just gonig to leave it on Amazon web services, and then you can also pick a plan. If you go to the development tab, there's a free sandbox, this is going to be more than enough for what we'll do in this course. You can also select the MongoDB version. Finally, pick a name for your database. I'm going to call this database flights. Click create.

To get the connection string for this database, click on the database name. Since we haven't used this database yet, it's prompting us to add a username and a password. So let's do that. On this line, they're providing us with the string we need to use to connect. You'll want to use the database user and database password you entered on the last screen. Now, we're ready to connect to this database. In our application, we'll be using a node module called Mongoose. This module will let us connect to the database and make queries against it.

For the moment, we'll just install Mongoose and get connected. Let's install Mongoose now. Go back to terminal, and then type npm install dash, dash save mongoose. Once mongoose finishes installing, open up the code in your favorite text editor. I am going to add a file here called db.js. In this file I am going to do three things. First, I am going to require mongoose. Next, I'm going to add that connection string.

To do that I'm going to call mongoose.connect. Now copy the connection string and paste it into db.js, now supply the database user name and password that you entered. Remember this is not the same user name that you used to login to mongolab.com. Finally let's export the connection. To do that, I'm going to type module exports, and then I'm going to set it to mongoose.connection. Now let's make sure that this works. Go to server.js, and require db.js.

Finally, let's start up the server and see if any errors occur. No errors are occurring, so we should be connected to the database. Mongoose manages a connection between a MongoDB database and your appliction. You can install MongoDB locally or you can use a service like MongoLab to host your database for you. In the next video, we'll create a schema for flight data.

Find answers to the most frequently asked questions about Node.js Essential Training .


Expand all | Collapse all
please wait ...
Q: How can I install the new version of the Express command-line tool?
A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Node.js Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.