I presented at the Milwaukee Ruby User’s Group tonight on NoSQL using MongoDB and Ruby.
Code Snippets for the Presentation
Basic Operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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.
Pingback: Tweets that mention NoSQL with MongoDB and Ruby Presentation | Zorched / One-Line Fix -- Topsy.com
Pingback: NoSQL Daily – Wed Nov 17 › PHP App Engine