Skip to content

Instantly share code, notes, and snippets.

@donnfelker
Created November 26, 2011 03:53
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 donnfelker/1394959 to your computer and use it in GitHub Desktop.
Save donnfelker/1394959 to your computer and use it in GitHub Desktop.
ruby_mongo_playground_issue
require 'mongo'
require 'csv'
# database connection
gamedb = Mongo::Connection.new("localhost", 27017).db("my_mongo_test")
games = gamedb.collection("games")
# Read through CSV file, find match in local db and then do work with the document...
CSV.foreach("/tmp/upc-game-dump-full-match.csv") do |row|
puts row[0] # Shows me the object ID that was in the CSV file, which originally came from mongodb
puts BSON::ObjectId.from_string( "#{row[0]}" ) # this fails, how do I parse an Object Id so I can query in Ruby with it?
# Basically I want to be able to do this: game = games.find_one( { "_id" => BSON::ObjectID.from_string("#{row[0]}") } )
# so I can get the document so I can work with it ... but my noob skills are failing me.
# Get the following error in the next file ...
end
/Users/donnfelker/.rvm/gems/ruby-1.9.2-p136@upc-etl/gems/bson-1.4.0/lib/bson/types/object_id.rb:126:in `from_string': illegal ObjectId format: _id (BSON::InvalidObjectId)
from migrate_upc_from_csv.rb:13:in `block in <main>'
from /Users/donnfelker/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/csv.rb:1768:in `each'
from /Users/donnfelker/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/csv.rb:1202:in `block in foreach'
from /Users/donnfelker/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/csv.rb:1340:in `open'
from /Users/donnfelker/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/csv.rb:1201:in `foreach'
from migrate_upc_from_csv.rb:9:in `<main>'
@refriedchicken
Copy link

You should be able to just do this:

game = games.find_one( { :_id => row[0]} )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment