Using MongoDB with Ruby: Getting Started

In the following subsections we shall introduce the Ruby Driver for MongoDB, and set up the environment with the required software.

1. Overview of the Ruby Driver for MongoDB

The Ruby driver for MongoDB API may be used in a Ruby script to connect to MongoDB Server and perform CRUD (create, read, update, and delete) operations on the server. The only namespace in the Ruby driver API is called Mongo. The main classes in the Mongo namespace are shown in Figure 4-1.

The Mongo namespace classes are discussed in Table 4-1.

2. Setting Up the Environment

We need to download and install the following software to access MongoDB Server from Ruby.

  • Ruby Installer for Ruby 2.1.6 (rubyinstaller-2.1.6-x64.exe). Download it from http://rubyinstaller.org/downloads/. MongoDB Ruby driver 2.x supports Ruby versions 1.8.7, 1.9, 2.0, and 2.1.
  • RubyInstaller Development Kit (DevKit). Download the appropriate version from http://rubyinstaller.org/downloads/. For Ruby 2.1.6 download DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe.
  • Ruby driver for MongoDB
  • MongoDB Server 3.0.5

3. Installing Ruby

To install Ruby double-click on the Ruby installer application rubyinstaller-2.1.6-x64.exe The Ruby

Setup wizard gets started.

  1. Select the Setup Language and click on OK.
  2. Accept the License Agreement and click on Next.
  3. In Installation Destination and Optional Tasks, specify a destination folder to install Ruby, or select the default folder. The directory path should not include any spaces. Select Add Ruby Executables to your PATH.
  4. Click on Install as shown in Figure 4-2.

Ruby starts installing as shown in Figure 4-3.

The Setup wizard completes installing Ruby as shown in Figure 4-4. Click on Finish.

Next, install Rubygems, which is a package management framework for Ruby. Run the following command to install Rubygems.

gem install rubygems-update

The Rubygems gem gets installed as shown in Figure 4-5.

If Rubygems is already installed, update to the latest version with the following command.

update_rubygems

Rubygems gets updated as shown in Figure 4-6.

4. Installing DevKit

DevKit is a toolkit that is used to build many of the C/C++ extensions available for Ruby.

  1. To install DevKit double-click on the DevKit installer application (DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe) to extract DevKit files to a directory. Change directories (cd) to the directory in which the files are extracted.

C:\Ruby21-x64

  1. Initialize DevKit and autogenerate the config.yml file using the following command.

ruby dk.rb init

  1. A config.yml file gets generated in the C:\Ruby21-x64 directory.

Add the following line to the config.yml file.

– C:/Ruby21-x64

  1. Install DevKit using the following command. ruby dk.rb install
  2. Verily that DevKit has been installed using the following command.

ruby -rubygems -e “require ‘json’; puts JSON.load(‘[42]’).inspect”

The output from the preceding commands to initialize/install DevKit are shown in the command shell in Figure 4-7.

5. Installing Ruby Driver for MongoDB

To install the MongoDB Ruby driver gem mongo, complete the following steps:

  1. Run the following command.

>gem install mongo

The MongoDB Ruby driver gem gets installed as shown in Figure 4-8.

  1. Next, install MongoDB Server. Only MongoDB versions 2.4, 2.6, and 3.x are compatible with MongoDB Ruby driver 2.x. Download MongoDB 3.0.5 from mongodb.org/ and extract the zip file to a directory.
  2. Add the bin directory from the MongoDB installation (for example, C:\Program Files\MongoDB\Server\3.0\bin) to the PATH environment variable.
  1. Start MongoDB server with the following command.

>mongod

MongoDB server gets started as shown in Figure 4-9.

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 *