Using MongoDB with Node.js: Getting Started

In the following subsections we shall introduce the Node.js driver for MongoDB and set up the environment.

1. Overview of Node.js Driver for MongoDB

The Node.js driver for MongoDB provides an API to connect to MongoDB server and perform different operations in the server such as adding a document or finding a document. The main classes in the Node.js driver are illustrated in Figure 5-1.

The Node.js driver classes are discussed in Table 5-1.

2. Setting Up the Environment

We need to install the following software for this chapter:

  • MongoDB
  • js
  • js driver for MongoDB

3. Installing MongoDB Server

Download MongoDB 3.0.5 from www.mongodb.org/ and extract the zip file to a directory. Add the bin directory from the MongoDB installation, for example, C:\Program Files\MongoDB\Server\3.0\bin to the PATH environment variable. Create the C:\data\db directory if not already created. Start MongoDB server with the following command.

>mongod

4. Installing Node.js

Download the node-v0.12.7-x64.msi application for Node.js fromblog.nodejs.org/release/ and complete the following steps:

  1. Double-click on the msi application to launch the Node.js Setup Wizard.
  2. Click on Next in the Setup Wizard as shown in Figure 5-2.
  3. Accept the End-User License Agreement and click on Next.
  4. In Destination Folder specify a directory to install Node.js in, the default being C:\Program Files\nodejs as shown in Figure 5-3. Click on Next.
  5. In Custom Setup, the Node.js features to be installed including the core Node. js runtime are listed for selection as shown in Figure 5-4. Choose the default settings and click on Next.
  1. In the Ready to Install Node.js window, click on Install as shown in Figure 5-5.

The installation of Node.js starts as shown in Figure 5-6. Wait for the installation to finish.

7. When the Node.js completes installing, click on Finish as shown in Figure 5-7.

  1. To find the version of Node.js installed, run the following command in a command shell.

node –version

The output from the command lists the version as 0.12.7 as shown in Figure 5-8.

  1. To test the Node.js installation, create a server using the following script; store the script in example.js in any directory, for example, the C:\Program Files\ nodejs\scripts directory.

var http = require(‘http’);

http.createServer(function (req, res) {

res.writeHead(200, {‘Content-Type’: ‘text/plain’});

res.end(‘Hello World\n’);

}).listen(1337, ‘127.0.0.1’);

console.log(‘Server running at http://127.0.0.1:1337/’);

  1. From the directory containing the script, run the script with the following command.

node example.js

The output from the script is shown in the command shell in Figure 5-9.

5. Installing the Node.js Driver for MongoDB

Open a new terminal/console and go to C:\Program Files\nodejs. Then to install the Node.js driver for MongoDB run the following command.

npm install mongodb

Node.js driver for MongoDB gets installed as shown in Figure 5-10.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *