I presented at the Milwaukee Ruby User's Group tonight on NoSQL using MongoDB and Ruby.
Code Snippets for the Presentation
Basic Operations
// insert data
db . factories . insert ( { name : "Miller" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Lakefront" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Point" , metro : { city : "Steven's Point" , state : "WI" } } );
db . factories . insert ( { name : "Pabst" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Blatz" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Coors" , metro : { city : "Golden Springs" , state : "CO" } } );
// simple queries
db . factories . find ()
db . factories . findOne ()
db . factories . find ( { "metro.city" : "Milwaukee" } )
db . factories . find ( { "metro.state" : { $in : [ "WI" , "CO" ] } } )
// update data
db . factories . update ( { name : "Lakefront" }, { $set : { thebest : true } } );
db . factories . find ()
// delete data
db . factories . remove ({ name : "Coors" })
db . factories . remove ()
Ruby Example
require 'rubygems'
require 'mongo'
include Mongo
db = Connection . new . db ( 'sample-db' )
coll = db . collection ( 'factories' )
coll . remove
coll . insert ( { :name => "Miller" , :metro => { :city => "Milwaukee" , :state => "WI" } } )
coll . insert ( { :name => "Lakefront" , :metro => { :city : "Milwaukee" , :state => "WI" } } )
coll . insert ( { :name => "Point" , :metro => { :city => "Steven's Point" , :state => "WI" } } )
coll . insert ( { :name => "Pabst" , :metro => { :city => "Milwaukee" , :state => "WI" } } )
coll . insert ( { :name => "Blatz" , :metro => { :city => "Milwaukee" , :state => "WI" } } )
coll . insert ( { :name => "Coors" , :metro => { :city => "Golden Springs" , :state => "CO" } } )
puts "There are #{ coll . count () } factories. Here they are:"
coll . find (). each { | doc | puts doc . inspect }
coll . map_reduce ( "function () { emit(this.metro.city, this.name); }" , "function (k, vals) { return vals.join(" , "); }" ). each { | r | puts r . inspect }
Map Reduce Example
db . factories . insert ( { name : "Miller" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Lakefront" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Point" , metro : { city : "Steven's Point" , state : "WI" } } );
db . factories . insert ( { name : "Pabst" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Blatz" , metro : { city : "Milwaukee" , state : "WI" } } );
db . factories . insert ( { name : "Coors" , metro : { city : "Golden Springs" , state : "CO" } } );
var fmap = function () {
emit ( this . metro . city , this . name );
}
var fred = function ( k , vals ) {
return vals . join ( "," );
}
res = db . factories . mapReduce ( fmap , fred )
db [ res . result ]. find ()
db [ res . result ]. drop ()
The Presentation
Download NoSQL with MongoDB and Ruby Slides
Thanks to Meghan at 10Gen for sending stickers and a copy of MongoDB: The Definitive Guide that I gave out as a door prize. I read the book quickly this weekend before the talk and found it quite good, so I recommend it if you want to get started with MongoDB.