Created
November 26, 2011 03:53
-
-
Save donnfelker/1394959 to your computer and use it in GitHub Desktop.
ruby_mongo_playground_issue
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 '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 |
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
/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>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should be able to just do this:
game = games.find_one( { :_id => row[0]} )