Created
August 19, 2009 14:07
-
-
Save jhancock/170361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mongo' | |
require 'pp' | |
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' | |
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT | |
puts "Connecting to #{host}:#{port}" | |
db = XGen::Mongo::Driver::Mongo.new(host, port).db('blog_test') | |
articles = db.collection('articles') | |
articles.clear() | |
article = { :title => "Mongo with Ruby is Fun!!", :content => "hello mongo programmers", | |
:url => "/articles/mongo_ruby", :comments => [ | |
{ "author" => "bob", "comments" => "hi comments", "status" => true }] } | |
articles << article | |
# next line shows our inserted Hash contains the new _id | |
puts "Article #{article[:_id]} inserted" | |
puts "Count of #{db}:#{articles} => #{articles.count}" | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $push to add to comments" | |
articles.update({"_id" => article[:_id]}, {"$push" => {"comments" => { "author" => "joe", "comments" => "hello", "status" => true } }}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $pushAll to add to comments" | |
articles.update({"_id" => article[:_id]}, | |
{"$pushAll" => {"comments" => [{ "author" => "jim", "comments" => "goodbye", "status" => false }, | |
{ "author" => "george", "comments" => "ni hao", "status" => false }]}}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $pull to remove from comments" | |
articles.update({"_id" => article[:_id]}, | |
{"$pull" => {"comments" => { "author" => "jim", "comments" => "goodbye", "status" => false }}}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $pullAll to remove from comments" | |
articles.update({"_id" => article[:_id]}, | |
{"$pullAll" => {"comments" => [{ "author" => "bob", "comments" => "hi comments", "status" => true }, | |
{ "author" => "george", "comments" => "ni hao", "status" => false }]}}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment