Skip to content

Instantly share code, notes, and snippets.

@durran
Created January 7, 2010 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durran/270961 to your computer and use it in GitHub Desktop.
Save durran/270961 to your computer and use it in GitHub Desktop.
[ durran@modetojoy-3 | ~/Work/mongoid ] > irb
irb(main):001:0> require "mongoid"
=> true
irb(main):002:0> class Person
irb(main):003:1> include Mongoid::Document
irb(main):004:1> field :age, :type => Integer
irb(main):005:1> end
=> nil
irb(main):006:0> conn = Mongo::Connection.new
=> #<Mongo::Connection:0x18d925c @slave_ok=nil, @queue=#<ConditionVariable:0x18d91a8>, @nodes=[["localhost", 27017]], @safe_mutex=#<Mutex:0x18d91bc>, @options={}, @size=1, @checked_out=[], @connection_mutex=#<Mutex:0x18d91d0>, @port=27017, @logger=nil, @id_lock=#<Mutex:0x18d9220>, @sockets=[], @timeout=5.0, @host="localhost">
irb(main):007:0> Mongoid.database = conn.db("sum_test")
=> #<Mongo::DB:0x18d4b80 @strict=nil, @pk_factory=nil, @connection=#<Mongo::Connection:0x18d925c @slave_ok=nil, @queue=#<ConditionVariable:0x18d91a8>, @nodes=[["localhost", 27017]], @safe_mutex=#<Mutex:0x18d91bc>, @options={}, @size=1, @checked_out=[], @connection_mutex=#<Mutex:0x18d91d0>, @port=27017, @logger=nil, @id_lock=#<Mutex:0x18d9220>, @sockets=[], @timeout=5.0, @host="localhost">, @name="sum_test">
irb(main):008:0> p = Person.create(:age => 30)
=> #<Person _id: 4b4558471ad6430af8000001, age: 30>
irb(main):009:0> Person.sum(:age)
=> 30.0
irb(main):010:0> Person.create(:age => 100)
=> #<Person _id: 4b45585c1ad6430af8000002, age: 100>
irb(main):011:0> Person.sum(:age)
=> 130.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment