mongodb

26 Oct

Node.js: Connection Pools and MongoDB

in javascript, mongodb, node.js

Node.js can interact well with MongoDB using the MongoDB native driver. The driver comes with plenty of documentation. However, one feature that isn't explained clearly is the pooling system.

The MongoDB Node library can create a pool of connections to the database that can then be shared throughout your Node application. That means that you can open a series of connections once (at startup) and then use those connections as needed.

Here are the mechanics:

mongodb = require('mongodb');
 
// Define options. Note poolSize.
var serverOptions = {
  'auto_reconnect': true,
  'poolSize': 5
};
 
// Now create the server, passing our options.
var serv = new mongodb.Server('localhost', 27017, serverOptions);
 
// At this point, there is no connection made to the server.
 
// Create a handle to the Mongo database called 'myDB'.
var dbManager = new mongodb.Db('myDB', serv);
 
// NOW we initialize ALL 5 connections:
dbManager.open(function (error, db) {
  // Do something with the connection.
 
  // Make sure to call db.close() when ALL connections need
  // to be shut down.
  db.close();
});

The salient detail of the above code is that the database connections are not opened until Db.open() is called. At that point, all five (our poolSize is 5) will be opened at once. These will be left open until the database is closed.

Internally, the MongoDB library will manage which requests go to which connections.

Note that the variables dbManager and db above both point to the same object. To really make use of pooling, your application will have to do something more sophisticated than the above. After all, there's no sense in opening five connections, executing a single operation, and then closing all five connections.

26 Mar

Loading Drupal Nodes into MongoDB with Drush

in drupal, mongodb, php, programming

To do some prototyping, I wanted to load all 32k of our Drupal nodes into MongoDB. At first, the thought of doing this seemed daunting. Then I realized that with Drush I could use a very simple script to perform an entire migration.

The result: With a 14 line PHP script, I transferred all of the nodes (CCK, taxonomy, and all) without a glitch.

Read on for the full explanation.

26 Mar

MapReduce as a Star Trek Episode

in mongodb

Kristina Chodorow, a member of the MongoDB development team, and maintainer of the Mongo PHP driver, wrote a great blog explaining Map Reduce as a Star Trek episode. It's a quick and humorous read.

Kristina is also doing a TEK-X Webinar on MongoDB today. I'm encouraging my dev team to attend.

05 Mar

MongoDB: 5 Things Every PHP Developer Should Know About MongoDB

in mongodb, php, programming

2010 will be remembered as the year SQL died; the year relational databases were moved off of the front line; the year that developers discovered that they no longer had to force every single object into a tabular structure in order to persist the data.

2010 is the year of the document database. While momentum has been steadily building over the last seven years or so, there are now a wide variety of stable document databases -- from cloud-based ones from Amazon and Google, to a wide variety of Open Source tools, most notably CouchDB and MongoDB.

So what is MongoDB? Here are five things every PHP developer should know about it:

  1. MongoDB is a stand-alone server
  2. It is document based, not table-based
  3. It is schemaless
  4. You don't need to learn another query language
  5. It has great PHP support

Read on to learn a little about each of these.

30 Jan

OS X: Installing MongoDB and the PHP Mongo Driver

in mac, mongodb, os x, php

MongoDB is a full-featured object database. Since it is fast, versatile, and schema-less, you can develop a very complex data storage layer without an ORM, and without any tedious coding. For this reason, I have been investigating MongoDB as a storage layer for PHP. Here's how to set up an environment on OS X Snow Leopard.

In this blog we'll do the following:

  • Install MongoDB
  • Add some initial data to MongoDB
  • Install the PHP PECL driver for MongoDB
  • Write a short PHP Script that uses MongoDB
  • Shut down the MongoDB server