Using MongoDB with Node.js

Before we officially implement MongoDB into our actual application, let's first take a look at some basic examples of connecting to a MongoDB server from within Node.js.

Create a new project folder to store some sample code to experiment with. I'll call my folder mongotest. Inside this folder, create a new file called test.js. In this file, we will play around with some code to test how to connect to MongoDB and how to insert and retrieve some data. The first thing we need to do in order to connect to a MongoDB server from Node.js is to require a mongodb module.

To get started, change directories into the new mongotest folder and install the mongodb module using npm:

    $ cd mongotest
    $ npm install mongodb
Don't be confused by the module's name. The mongodb npm module isn't MongoDB itself, but rather a third-party npm module that facilitates communicating to a MongoDB server from within Node.js. Also, because this is just a sample project to experiment with, we don't require the --save flag with npm install, since we aren't maintaining a package.json file.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.168