Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6859451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:17:05+00:00 2026-05-27T02:17:05+00:00

I have some code written in Ruby 1.9.2 patch level 136 and I’m have

  • 0

I have some code written in Ruby 1.9.2 patch level 136 and I’m have an issue where when I perform a find via the _id in the raw ruby mongo driver I get a nil when trying to use a value from a csv file. Here’s the code:

require 'mongo'
require 'csv'
require 'bson'

# Games database
gamedb = Mongo::Connection.new("localhost", 27017).db("gamedb")
@games = gamedb.collection("games")

# Loop over CSV data.
CSV.foreach("/tmp/somedata.csv") do |row|

  puts row[0] # Puts the ObjectId

  @game = @games.find( { "_id" => row[0] } ).first  
  puts @game.inspect

end

The CSV file looks like this:

_id,game_title,platform,upc_db_match,upc
4ecdacc339c7d7a2a6000002,TMNT,PSP,TMNT,085391157663
4ecdacc339c7d7a2a6000004,Super Mario Galaxy,Wii,Super Mario Galaxy,045496900434
4ecdacc339c7d7a2a6000005,Beowulf,PSP,Beowulf,097363473046

The first column is the objectId in Mongo that I already have. If I perform a local find from the mongo command line the values in the first column, I get the data I want. However, the code above returns nil on the @game.inspect call.

I’ve tried the following variations, which all produce nil:

@game = @games.find( { "_id" => row[0].to_s } ).first
@game = @games.find( { "_id" => row[0].to_s.strip } ).first

I’ve even tried building the ObjectId with the BSON classes as such:

@game = @games.find( { "_id" => BSON::ObjectId(row[0]) } ).first

or

@game = @games.find( { "_id" => BSON::ObjectId("#{row[0]}") } ).first

Both of which output the following error:

/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 /Users/donnfelker/.rvm/gems/ruby-1.9.2-p136@upc-etl/gems/bson-1.4.0/lib/bson/types/object_id.rb:26:in `ObjectId'
    from migrate_upc_from_csv.rb:14: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:10:in `<main>'

The crazy thing is, if I manually create the BSON ObjectId by hand it works (as shown below):

@game = @games.find( { "_id" => BSON::ObjectId("4ecdacc339c7d7a2a6000004") } ).first

When I run @game.inspect I get my data back, as I would expect. However, If I change this to use row[0], I get nil.

Why? What am I doing wrong?

System Details

$ gem list

*** LOCAL GEMS ***

bson (1.4.0)
bson_ext (1.4.0)
mongo (1.4.0)

RVM Version: rvm 1.6.9

Ruby Version: ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0]

Mongo Version:

[initandlisten] db version v1.8.2, pdfile version 4.5
[initandlisten] git version: 433bbaa14aaba6860da15bd4de8edf600f56501b

Again, why? What am I doing wrong here? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:17:06+00:00Added an answer on May 27, 2026 at 2:17 am

    The first row is not being read as a header, to do that pass in :headers => true like this:

    require 'csv'
    
    # Loop over CSV data.
    CSV.foreach("/tmp/somedata.csv", :headers => true) do |row|
    
      puts row[0] # Puts the ObjectId
    
    end
    

    If you do not pass the :headers parameter in you can see the first row[0] object is the string “_id”:

    _id
    4ecdacc339c7d7a2a6000002
    4ecdacc339c7d7a2a6000004
    4ecdacc339c7d7a2a6000005
    

    When you include it, you are golden:

    4ecdacc339c7d7a2a6000002
    4ecdacc339c7d7a2a6000004
    4ecdacc339c7d7a2a6000005
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some Ruby code to import the Google n-gram data into a
I have written some code to fetch zipcodes from a mysql db via ajax
I have some code I've written in PHP for consuming our simple webservice, which
I have written some code in my VB.NET application to send an HTML e-mail
I have written some code for playing a .wav through my application. Now I
I have written some code to ensure that items on an order are all
I have written some code to look at properties using reflection. I have retrieved
I have written some code to display a YouTube video on my page but
I have written some WebGL code, actually I am playing with the examples that
I have inherited some legacy PHP code what was written back when it was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.