Created
August 17, 2009 17:31
-
-
Save jhancock/169258 to your computer and use it in GitHub Desktop.
This file contains hidden or 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", :tags => ["ruby"] } | |
articles << article | |
# next line shows our inserted Hash contains the new _id | |
puts "Article #{article[:_id]} inserted" | |
puts "Count of #{db.name}:#{articles.name} => #{articles.count}" | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $push to add to tags" | |
articles.update({"_id" => article[:_id]}, {"$push" => {"tags" => "mongo"}}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $pushAll to add to tags" | |
articles.update({"_id" => article[:_id]}, {"$pushAll" => {"tags" => ["merb", "programming"]}}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $pull to remove from tags" | |
articles.update({"_id" => article[:_id]}, {"$pull" => {"tags" => "programming"}}) | |
puts "Articles:" | |
articles.find().each { |doc| pp doc } | |
puts | |
puts "use $pullAll to remove from tags" | |
articles.update({"_id" => article[:_id]}, {"$pullAll" => {"tags" => ["merb", "ruby"]}}) | |
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