Xervo

Node.js Tutorial: Using MongoDB

Node.js Tutorial: Using MongoDB

In this tutorial you learn how to use MongoDB with Node.js. MongoDB is an increasingly popular document-based, high-performance NoSQL database.

Using MongoDB with Node.js

In this tutorial you learn how to use MongoDB with Node.js. MongoDB is an increasingly popular document-based, high-performance NoSQL database. In MongoDB, data is stored as a document as a set of key-value pairs. You can define multiple databases in MongoDB where each database can have many collections, and those collections are simply a set of documents that consist of data stored as a key-value pair.

The data structure that defines a MongoDB document is called BSON (Binary JSON). BSON is binary representation of JSON and also supports data types such as Date, which is not supported in plain JSON format. MongoDB internally converts JSON to BSON and vice versa for performance benefits, although the user can save, query and retrieve as JSON.

Making a Connection with MongoDB in Node.js

To work with the database, you first need to create a connection. In this section of the tutorial, we will be using MongoDB’s native Node.js driver to create the connection with the MongoDB server. To install the mongodb native drivers, use the npm command to install the mongodb module. After that, run the following command in your project directory.

> npm install mongodb

npm is a package manager that provides a central repository for custom open source modules for Node.js and JavaScript. npm makes it simple to manage modules, their versions and distribution. As shown in the preceding paragraph, the npm install command was used to install the required module in our project.

Next, you should create a new blank JS file in the project directory and write the following code into it:

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/my_database_name';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);

    // do some work here with the database.

    //Close connection
    db.close();
  }
});

Now assuming your database is running on the url mentioned above (in this case it’s localhost) and then running the above file using node, we should be able to connect to the database server.

Analyzing the preceding code we can break the above program into the following logical steps: ● Load the mongodb module: We used require to load the mongodb module in our code. mongodb module represents the native mongodb drivers for Node.js.

 //lets require/import the mongodb native drivers.
  var mongodb = require('mongodb');

● Defining the URL we need to connect to: We need to know where our mongodb server is running. The url represents the location where the mongodb server instance is running such that we can connect to it. The url contains the database name to which we intend to connect.

  // Connection URL. This is where your mongodb server is running.
  var url = 'mongodb://localhost:27017/my_database_name';

● Connect to the database: Let’s use the MongoClient interface to connect to the database. In the callback we get error or the db object. We use the db object in order to communicate with the database.

 //We need to work with "MongoClient" interface in order to connect to a mongodb server.
  var MongoClient = mongodb.MongoClient;

  // Use connect method to connect to the Server
  MongoClient.connect(url, function (err, db) {
    if (err) {
      console.log('Unable to connect to the mongoDB server. Error:', err);
    } else {
      //HURRAY!! We are connected. :)
      console.log('Connection established to', url);

      // do some work here with the database.

      //Close connection
      db.close();
  }

Inserting / Updating and Querying on MongoDB database

Now that you have a db connection ready, it’s time to communicate with the database. Let’s take a look at how we can perform some basic operations on the MongoDB database.

There are a couple of things to keep in mind when using the mongodb native driver:

1. The query function names and their parameters are similar to that of native mongodb commands.

2. All these query functions take the callback as the last argument. The callback function gives the first argument as error and the second argument as result. Result is actually result/output provided by the mongodb on running these commands. It is similar to what you see if you had run these queries in the mongodb shell.

Let’s assume that we need to create a users collection and save a few users in it. For this we will use the insert method to save the user's info.

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/my_database_name';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);

    // Get the documents collection
    var collection = db.collection('users');

    //Create some users
    var user1 = {name: 'modulus admin', age: 42, roles: ['admin', 'moderator', 'user']};
    var user2 = {name: 'modulus user', age: 22, roles: ['user']};
    var user3 = {name: 'modulus super admin', age: 92, roles: ['super-admin', 'admin', 'moderator', 'user']};

    // Insert some users
    collection.insert([user1, user2, user3], function (err, result) {
      if (err) {
        console.log(err);
      } else {
        console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result);
      }
      //Close connection
      db.close();
    });
  }
});

In the preceding code we did the following steps: ● Get the reference of our 'users' collection: We used db.collection method to get the reference to our users collection. If the collection does not exist, then it will be created on the first insert. If it already exists, then you will be working on the existing one. You don't need to create a collection explicitly.

	 // Get the documents collection
  var collection = db.collection('users');

Prepare data to be inserved: We created some sample user record objects named user1,user2 and user3. There is no fixed schema of the document, they are simple JS Objects with entity's data.

Insert into the database: We used collection.insert to insert our data in the database collection. We passed an Array of users to it and it saved all of them in one step. If we need to save only one document (one user's info), then we can pass that in the insert method as it is, i.e. collection.insert(user1, callback).

	 // Insert some users
  collection.insert([user1, user2, user3], function (err, result) {
    if (err) {
      console.log(err);
   } else {
      console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result);
    }
    //Close connection
    db.close();
  });

Callback: The callback function for the insert method has two parameters, first is error and other is result. If there is an error, then error object will have the error details or else it will be null. result will be an array of documents saved.

Updating the documents in MongoDB

To update the records we use collection.update method. The name and parameters of the update method are similar to that of native mongodb queries. Let’s take a look at an example below:

// Get the documents collection
var collection = db.collection('users');

// Insert some users
collection.update({name: 'modulus user'}, {$set: {enabled: false}}, function (err, numUpdated) {
  if (err) {
    console.log(err);
  } else if (numUpdated) {
    console.log('Updated Successfully %d document(s).', numUpdated);
  } else {
    console.log('No document found with defined "find" criteria!');
  }
  //Close connection
  db.close();
});

Querying the documents in MongoDB

Next, let’s retrieve the documents from mongodb. To do this we can fire the find command on the collection object as we do in the mongodb shell. Here is an example below:

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/my_database_name';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);

    // Get the documents collection
    var collection = db.collection('users');

    // Insert some users
    collection.find({name: 'modulus user'}).toArray(function (err, result) {
      if (err) {
        console.log(err);
      } else if (result.length) {
        console.log('Found:', result);
      } else {
        console.log('No document(s) found with defined "find" criteria!');
      }
      //Close connection
      db.close();
    });
  }
});

In the preceding example you can see we used .toArray chained to find because running find alone returns a db cursor. We then would have to iterate over the cursor and fetch results. The reason for this is that for performance reasons, mongodb does not fetch the full record data by default. Using toArray tells the driver that we want full data of each user, which is then returned.

Example using DB cursor

Let’s see how we can use the db cursor and work with our find result.

//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');

//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;

// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/my_database_name';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to', url);

    // Get the documents collection
    var collection = db.collection('users');

    //We have a cursor now with our find criteria
    var cursor = collection.find({name: 'modulus user'});

    //We need to sort by age descending
    cursor.sort({age: -1});

    //Limit to max 10 records
    cursor.limit(10);

    //Skip specified records. 0 for skipping 0 records.
    cursor.skip(0);

    //Lets iterate on the result
    cursor.each(function (err, doc) {
      if (err) {
        console.log(err);
      } else {
        console.log('Fetched:', doc);
      }
    });
  }
});

Instead of each we can also use toArray to return the full array of all documents as in the previous example. Using each will load the documents with each iteration. There are other methods like remove to delete documents, count to count the documents, and more. You can have a look on the complete documentation here.

Working with MongoDB using Mongoose ODM module in Node.js

MongooseJS is a very popular library in Node.js which provides a straightforward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more--out of the box. Using mongoose you can work with mongodb like a pro. Mongoose makes it simple to work with mongodb using its interface features and also increases the reliability with features like automatic connection and connection pool management. To work with mongoose we need to install the mongoose module and for that we will use npm. Run the following command to install the mongoose module:

> npm install mongoose

Now let’s try to write the above example using mongoose:

//Lets load the mongoose module in our program
var mongoose = require('mongoose');

//Lets connect to our database using the DB server URL.
mongoose.connect('mongodb://localhost/my_database_name');

/**
 * Lets define our Model for User entity. This model represents a collection in the database.
 * We define the possible schema of User document and data types of each field.
 * */
var User = mongoose.model('User', {name: String, roles: Array, age: Number});

/**
 * Lets Use our Models
 * */

//Lets create a new user
var user1 = new User({name: 'modulus admin', age: 42, roles: ['admin', 'moderator', 'user']});

//Some modifications in user object
user1.name = user1.name.toUpperCase();

//Lets try to print and see it. You will see _id is assigned.
console.log(user1);

//Lets save it
user1.save(function (err, userObj) {
  if (err) {
    console.log(err);
  } else {
    console.log('saved successfully:', userObj);
  }
});

In the preceding code we did the following: ● Connected to the database: We used the mongoose.connect method to connect to the database desired. The mongooseJS will then internally handle the connection and connection pool as configured. ● Created a Model: We created a Model using mongoose.model method. Model represents a collection in the database. The name you provide to the model will be pluralized internally and the collection with that plural name will be created in mongodb. For example: In our case if we name our model User, then the collection will be named users all lowercase. We also define the schema for a document during this process.

	 var User = mongoose.model('User', {name: String, roles: Array, age: Number});

● Created a new User Object: We created a new User object using our model. We can create as many user objects using our model, and each object will represent a document in database. We also modified the object and saved it.

	//Lets create a new user
var user1 = new User({name: 'modulus admin', age: 42, roles: ['admin', 'moderator', 'user']});
//Some modifications in user object
user1.name = user1.name.toUpperCase();

● Saved the user using the 'save' method: Mongoose model has many methods available on the entity objects. In this case, we used the save method to save the document in the database. We can also use the Model to query database. To query the database collection we can use the Model defined to do that. Let’s see an example below where we try to find a user and then update it:

//Lets load the mongoose module in our program
var mongoose = require('mongoose');

//Lets connect to our database using the DB server URL.
mongoose.connect('mongodb://localhost/my_database_name');


/**
 * Lets define our Model for User entity. This model represents a collection in the database.
 * We define the possible schema of User document and data types of each field.
 * */
var User = mongoose.model('User', {name: String, roles: Array, age: Number});

//Lets try to Find a user
User.findOne({name: 'modulus admin'}, function (err, userObj) {
  if (err) {
    console.log(err);
  } else if (userObj) {
    console.log('Found:', userObj);

    //For demo purposes lets update the user on condition.
    if (userObj.age != 30) {
      //Some demo manipulation
      userObj.age += 30;

      //Lets save it
      userObj.save(function (err) {
        if (err) {
          console.log(err);
        } else {
          console.log('Updated', userObj);
        }
      });
    }
  } else {
    console.log('User not found!');
  }
});

For more details on Mongoose JS you can have a look on its documentation here.

Conclusion

In this tutorial you learned how to use the native MongoDB Node.js drivers to interact with the database. We saw how we can do basic insert/update and query operations with little introduction to db cursors. You also learned about basics of MongooseJS and how to get started with MongooseJs.

What is Xervo?

Xervo makes deploying applications in the public cloud or your own data center easy. Node.js, PHP, Java, Python, Nginx, and MongoDB supported. Full Docker support included in Enterprise version. It’s free to get started.

Share This Article

comments powered by Disqus