William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Tag Archives: Mongodb

A rails+mongo Gemfile sample

A rails+mong Gemfile sample

While I learn Rails, I found a good sample to combine MongoDB into Rails, I list here for quick retrieving:

source 'https://rubygems.org'
require 'mongo'

gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.1'

group :development, :test do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  gem 'mongo_mapper', '~> 0.12.0'
  gem 'bson_ext', '~> 1.8.2'
  gem 'bson', '~> 1.8.2'
  gem 'mongo', '~> 1.8.2'

  gem 'uglifier', '>= 1.0.3'

  gem "rspec-rails", "~> 2.0"

gem 'jquery-rails'

Of course, need to install (probably need root permission):

$ bundle install

After successfully install, I created a config file: #{app}/config/initializers/mongo_config.rb to initialize it:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "test"
if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
  MongoMapper.connection.connect if forked

After the config, right now when Webrick starts, the MongoDB is associated to connect.

Mongoctl: for MongoDB Replica Set.

Mongoctl: MongoDB Replica Set Clusters

According to mongoctl website:

mongoctl is a lightweight command line utility that simplifies the installation of MongoDB and management of MongoDB servers and replica set clusters. It is particularly useful if you maintain many MongoDB environments with lots of configurations to manage.

Following the instruction, I installed it in CentOS 6.3 like this:

$ mkdir tools && cd tools
$ git clone https://github.com/pypa/pip.git
$ cd pip
$ sudo python setup.py install
$ type pip
$ sudo pip install mongoctl
// then:
$ mongoctl list-servers
$ mongoctl status MyServer

Auto-suggest and MongoDB

Auto-suggest and MongoDB

Suppose we have a search form, we want to add auto-suggest feature in the search field. How to do that?

If using MySQL, there will be time-delay, the speed performance is not good enough. A easy improvement is to mix MongoDB and MySQL: save most visited keys in MongoDB.

Everytime when user key-in, check MongoDB first and return if found; if not found, then search in MySQL, and save a copy in MongoDB too.
This way is quite efficient. The following is the codes (PHP snippet, very quickly).

try {
  // connect mongoDB server: localhost:27017
  $m = new Mongo();
  // select a database
  $db = $m->auto_suggest;
catch ( MongoConnectionException $e ) {
  die('Couldn\'t connect to mongodb, is the "mongoD" process running?');

$ary = array();
$collection = $db->search;

if(!empty($_GET['q'])) {
  $q = trim($_GET['q']);

  $regex = new MongoRegex("/$q/i");
  $cursor = $collection->find(array('key'=> $regex));

  $it = iterator_to_array($cursor);
  if(! empty($it)) {
    $count = 1;
    foreach($cursor as $c) {
      array_push($ary, iconv('UTF-8', 'UTF-8//TRANSLIT', $c{'key'}));
      if( ++$count > 10) break;
  if(!empty($ary)) {
	echo json_encode($ary);
  else {
    $mydb = mysql_pconnect(...) or die(mysql_error());
    mysql_select_db('demo', $mydb);
    mysql_query("SET NAMES 'utf8'", $mydb);

    // use full-text and match()...against() to improve.
    $query1 = "select keyword from keywords where keyword like '%" . $q . "%' order by keyword";
    array_push_array($ary, mysql2mongo($collection, $query1, $mydb));
    echo json_encode($ary);

function mysql2mongo($c, $sql, $mydb)
  $a = array();
  $res = mysql_query($sql, $mydb) or mysql_error();
  if(mysql_num_rows($res)>0) {
    while($row = mysql_fetch_array($res, MYSQL_NUM)) {
      $t = iconv('UTF-8', 'UTF-8//TRANSLIT', $row[0]);
      $a[] = $t;
      $obj = array( 'key' => $t, 'count' => 1 );
  return $a;
function array_push_array(&$arr) {
  $args = func_get_args();
  if (!is_array($arr)) {
	trigger_error(sprintf("%s: Cannot perform push on something 
          that isn't an array!", __FUNCTION__), E_USER_WARNING);
	return false;
  foreach($args as $v) {
	if (is_array($v)) {
	  if (count($v) > 0) {
		array_unshift($v, &$arr);
		call_user_func_array('array_push',  $v);
    else $arr[] = $v;
  return count($arr);

Some explains:

  1. This script is triggered via ajax/json, and return json data(hash/associated array)
  2. When user is typing in(key-up event), check the http request($_GET) value in both MongoDB and MySQL, according to the sequence.
    MongoDB like cache, quicker than MySQL, so check first. If not found, then check MySQL.
  3. if $_GET value exists in MongoDB’s title field, then push as much as 10 latest results into $ary array, and return this $ary(json format) immediately.
  4. if not found in MongoDB, then search in MySQL.
  5. if found in MySQL, save a copy in MongoDB, so next time this key will directly be fetched from MongoDB, thus quicker.
  6. if not found in MySQL, then discard it.
    This way the search will be most efficient and high performance.

CentOS 6.2: Install mongoDB server and PHP mongo extension

CentOS 6.2: Install mongoDB server and PHP mongo extension

Here I summary the steps of installing mongoDB server and PHP mongo extension which are working on CentOS 6.2.

A. Install mongoDB server

To install mongoDB server, a good reference is here. My steps are:
(1) Following the instruction, use ‘yum’ to do the installation.
$ sudo vi /etc/yum.repos.d/10gen.repo

name=10gen Repository

(2) $ yum install mongo-10gen mongo-10gen-server to install mongoDB server.

(3) The configure is reside on /etc/mongod.conf, and the start/stop/restart script is /etc/rc.d/init.d/mongod. To start the service:
$ sudo service mongod start, or:
$ sudo /etc/init.d/mongod restart

Make sure the daemon works properly: $ sudo chkconfig mongod on

(4) It is running now. This MongoDB instance will store its data files in the /var/lib/mongo and its log files in /var/log/mongo, and run using the mongod user account.
Next step we are going to install PHP mongo extension in case to access the server.

B. Install and configure PHP mongo extension

(1) Using pecl(http://pecl.php.net/) is a simpler and effective way, instead of others such as wget & configure, or yum.Basically pecl does the following things: wget, phpize, ./configure, make && make test && make install.
$ sudo pecl install mongo
The mongo extension is now installed.

(2) Now add mongo.so to php.ini as a PHP extension.
$ sudo vi /etc/php.ini:


(3) Restart http server to make the change is active.
$ sudo service httpd restart

(4) Write a test script to make sure the installation and configure are right. (or using phpinfo to check).
vi test_mongo.php:

$m = new Mongo();
and return:
Mongo Object
    [connected] => 1
    [status] => 
    [server:protected] => 
    [persistent:protected] => 

Now a new mongo cache system is ready to use.

Install MongoDB with Windows XAMPP

Install MongoDB with Windows XAMPP

I found a useful article How to install MongoDB to use with XAMPP on Windows when I struggled with the correct version of php_mongo.dll.

Installing MongoDB

  1. Download MongoDB from http://www.mongodb.org/downloads
  2. Extract the archive
  3. Open Command Prompt
  4. Goto mongodb\bin folder
  5. Type to create DB repository and start daemon:
mkdir C:\mongodb\db
mongod.exe --dbpath=C:\mongodb\db

Check your Php version by going to http://localhost/xampp/phpinfo.php
note down the following from the below image
first the php version(here 5.3) then the php extension build (here ts,vc6)

  1. Download MongoDB driver from github.com/mongodb/mongo-php-driver/downloads
  2. Extract driver and copy the dll that matches your php version installed on your xampp (here mongo-1.1.4-php5.3vc6ts) and put to php/ext folder
  3. Open php.ini file
  4. Append this extension=php_mongo.dll, save php.ini file, and then restart your web server.
if every thing went fine then 
  1. Point your browser at localhost and select your language, then phpinfo() from the left menu
  2. Make sure there’s a Mongo section
The core is the ‘PHP Extension Build: API20090626,TS,VC6‘ option in localhost/xampp/phpinfo.php, the ‘TS,VC6‘ here is important for download correct php_mongo.dll version, others corrupt.

Step 3

Try out this example from here http://www.php.net/manual/en/mongo.tutorial.php to test mongodb is working