first look at mongodb

Jul 16, 2010

Here’s what I learned from today’s experimentation with MongoDB. Kudos to 10gen for the wonderful documentation. Everything I needed to know about: from installation, to how it works, to what commands to use, were all right there in the docs. There was even a SQL to Mongo Mapping Chart that helps SQL users translate MongoDB’s commands and query statements. Here’s a SQL to Mongo mapping of the storage structure:

SQLMongoDB
databasedatabase
tablecollection
rowdocument
columnfield
primary key_id

Using the mongo gem in an irb session, I learned about the classes in MongoDB.

MongoDB Class NameDescription
Mongo::ConnectionConnection object holds a specific connection to the MongoDB server
Mongo::DBDatabase object holds a specific database
Mongo::CollectionCollection object holds a specific collection
Mongo::CursorCursor object holds a set of documents from a specified query
BSON::OrderedHashOrderedHash object holds a document

[~] irb
ruby-1.8.7-p299 > require 'rubygems'
ruby-1.8.7-p299 > require 'mongo'
ruby-1.8.7-p299 > include Mongo
ruby-1.8.7-p299 > connection = Connection.new
ruby-1.8.7-p299 > db = connection.db('test_db')
ruby-1.8.7-p299 > collection = db.collection('test_coll')
ruby-1.8.7-p299 > collection.insert({'name' => 'sam', 'animal' => 'dog'})
ruby-1.8.7-p299 > collection.insert({'name' => 'dixie', 'animal' => 'cat', 'breed' => 'maneki neko'})
ruby-1.8.7-p299 > collection.insert({'name' => 'dixie', 'animal' => 'fish'})
ruby-1.8.7-p299 > cursor = collection.find
ruby-1.8.7-p299 > cursor.to_a[0]["name"]
 => "sam"

Note: I took out the return values for most of the commands in the IRB session.

Once you have a collection object, there are many ways to query the collection. The basic query command is the find command as shown above. Without any arguments, it returns the entire collection as a Mongo::Collection object. With arguments, you can specify which documents you want returned in a cursor (Mongo::Cursor).

Find a document object with a ‘name’ field, and ‘dixie’ value.


ruby-1.8.7-p299 > collection.find({'name' => 'dixie'})
 => <Mongo::Cursor:0x8094bb0c namespace='test_db.test_coll' @selector={"name"=>"dixie"}>

You can search using any field.


ruby-1.8.7-p299 > collection.find({'animal' => 'dog'})
 => <Mongo::Cursor:0x80949488 namespace='test_db.test_coll' @selector={"animal"=>"dog"}>

You can search against multiple fields to get a more refined search.


ruby-1.8.7-p299 > collection.find({'name' => 'dixie', 'animal' => 'fish'})
 => <Mongo::Cursor:0x809414f4 namespace='test_db.test_coll' @selector={"name"=>"dixie", "animal"=>"fish"}>

If not searching with exact values, you can use regular expressions or conditional operators.


ruby-1.8.7-p299 > collection.find({'name' => /^d/})
 => <Mongo::Cursor:0x8090b048 namespace='test_db.test_coll' @selector={"name"=>/^d/}>

I plan to write all of the possible moves in a 4x4 Tic Tac Toe game and export the collection as a *.bson file.


{'board' => [], 'best_moves' => []}

Backup to *.bson file


[~/local/mongodb/backup] mongodump --db test_db --collection test_coll
connected to: 127.0.0.1
DATABASE: test_db	 to 	dump/test_db
	test_db.test_coll to dump/test_db/test_coll.bson
		 3 objects