Skip to content

Instantly share code, notes, and snippets.

@shafferj
Created August 6, 2009 16:32
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 shafferj/163409 to your computer and use it in GitHub Desktop.
Save shafferj/163409 to your computer and use it in GitHub Desktop.
the following has been happening, so started to dig into the implementation of find methods:
confirm data in mongo:
> db.users.find()
{"_id" : ObjectId( "f3df7a4a0388833cde000000") , "created_at" : "Thu Aug 06 2009 09:51:47 GMT-0400 (EDT)" , "updated_at" : "Thu Aug 06 2009 09:51:47 GMT-0400 (EDT)" , "email" : "blah"}
{"_id" : ObjectId( "98e17a4ab87e084b55000000") , "created_at" : "Thu Aug 06 2009 09:58:48 GMT-0400 (EDT)" , "updated_at" : "Thu Aug 06 2009 09:58:48 GMT-0400 (EDT)" , "email" : "blah"}
{"_id" : ObjectId( "05e77a4a2b4c347d84000000") , "created_at" : "Thu Aug 06 2009 10:21:57 GMT-0400 (EDT)" , "updated_at" : "Thu Aug 06 2009 10:21:57 GMT-0400 (EDT)"}
{"_id" : ObjectId( "71f27a4a66b0febbc7000000") , "created_at" : "Thu Aug 06 2009 11:10:41 GMT-0400 (EDT)" , "updated_at" : "Thu Aug 06 2009 11:10:41 GMT-0400 (EDT)" , "email" : "test@test.com"}
{"_id" : ObjectId( "4ff87a4a62f037844a000000") , "created_at" : "Thu Aug 06 2009 11:35:43 GMT-0400 (EDT)" , "updated_at" : "Thu Aug 06 2009 11:35:43 GMT-0400 (EDT)" , "email" : "f@this.com"}
>
Class def for User(scaled back from implementation for debugging)
class User
include MongoMapper::Document
def initialize
end
def to_json --not relevant for this case, but there so i thought id leave it.
j = Hash.new
j['id'] = @_id.to_s
j['activity'] = { 'joinedAt' => @created_at.to_i, 'stats' => {}, 'likes' => {}}
j.to_json
end
key :email, String
end
justin-shaffers-macbook-pro:hotpotato-web-prototype shafferj$ script/console
Loading development environment (Rails 2.3.3)
>> User.all
{}
ArgumentError: wrong number of arguments (1 for 0)
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/jnunemaker-mongomapper-0.3.1/lib/mongomapper/document.rb:162:in `new'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/jnunemaker-mongomapper-0.3.1/lib/mongomapper/document.rb:162:in `block in find_every'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/jnunemaker-mongomapper-0.3.1/lib/mongomapper/document.rb:162:in `map'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/jnunemaker-mongomapper-0.3.1/lib/mongomapper/document.rb:162:in `find_every'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/jnunemaker-mongomapper-0.3.1/lib/mongomapper/document.rb:62:in `all'
from (irb):1
from /opt/local/bin/irb:12:in `<main>'
here is the code from document.rb:162
def find_every(options)
criteria, options = FinderOptions.new(options).to_a
162>> collection.find(criteria, options).to_a.map { |doc| new(doc) }
end
if i run the following, it works, as you can see.
>> cursor = User.collection.find({},{:fields=>nil, :offset=>0, :limit=>1, :sort=>{:$natural=>1}})
=> #<XGen::Mongo::Driver::Cursor:0x25bea80 @query=#<XGen::Mongo::Driver::Query:0x25bea94 @hint=nil, @order_by={:$natural=>1}, @number_to_return=1, @number_to_skip=0, @explain=nil, @selector={}, @fields=nil>, @collection=#<XGen::Mongo::Driver::Collection:0x74b030 @name="users", @db=#<XGen::Mongo::Driver::DB:0x2515f20 @nodes=[["localhost", 27017]], @name="hotpotato_development", @strict=nil, @pk_factory=nil, @slave_ok=nil, @auto_reconnect=nil, @semaphore=#<Object:0x2515ef8 @_mutex=#<Mutex:0x2515aac>>, @socket=#<TCPSocket:0x2515a84>, @port=27017, @host="localhost">, @hint=nil>, @db=#<XGen::Mongo::Driver::DB:0x2515f20 @nodes=[["localhost", 27017]], @name="hotpotato_development", @strict=nil, @pk_factory=nil, @slave_ok=nil, @auto_reconnect=nil, @semaphore=#<Object:0x2515ef8 @_mutex=#<Mutex:0x2515aac>>, @socket=#<TCPSocket:0x2515a84>, @port=27017, @host="localhost">, @num_to_return=1, @cache=[], @closed=false, @can_call_to_a=true, @query_run=false, @rows=nil>
>> puts cursor.next_object.inspect
{"_id"=>#<XGen::Mongo::Driver::ObjectID:0x25bb844 @data=[243, 223, 122, 74, 3, 136, 131, 60, 222, 0, 0, 0]>, "created_at"=>2009-08-06 13:51:47 UTC, "updated_at"=>2009-08-06 13:51:47 UTC, "email"=>"blah"}
this is whats getting passed into that block as doc
>> User.collection.find({},{:fields=>nil, :offset=>0, :limit=>1, :sort=>{:$natural=>1}}).to_a.map
=> #<Enumerator:0x3daba8>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment