Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-739

Inserting records w/ Duplicate Symbol/String Keys

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.10.0
    • Affects Version/s: 1.9.2
    • Component/s: BSON
    • Labels:
      None

      If a hash contains a string and a symbol in different keys which are equivalent, it appears that it's possible for both of these values to be inserted into the database.

      This appears to be perfectly valid in the JSON spec:
      http://www.ietf.org/rfc/rfc4627.txt
      " The names within an object SHOULD be unique."

      But, this creates buggy issues for database applications.

      Here is a snippet of code which can be used to insert duplicate keys- with conflicting values- into a Mongo collection

      #!/usr/bin/env ruby

      require 'mongo'
      DB = Mongo::MongoClient.new("localhost",27017).db("CR")
      before = Hash.new
      before['_id'] = 'duplicate_test'
      before['i_am_a_key'] = 'am i evil?'
      before[:i_am_a_key] = 'yes i am!'
      DB['erase_me'].insert(before)
      after = DB['erase_me'].find_one({_id:'duplicate_test'})

      puts("before inserting: --> #

      {before}

      ")
      puts("after inserting: --> #

      {after}

      ")

      Will generate:
      before inserting: -->

      {"_id"=>"duplicate_test", "i_am_a_key"=>"am i evil?", :i_am_a_key=>"yes i am!"}

      after inserting: -->

      {"_id"=>"duplicate_test", "i_am_a_key"=>"yes i am!"}

      This may seem expected, however if we perform a mongoexport:

      /var/www/html/ted/steve/rubycode

      {devel}

      06:34 PM $ mongoexport -h localhost -d CR -c erase_me | grep "_id"
      connected to: 172.16.10.55:27017,172.16.20.55:27017,172.16.20.56:27017
      exported 1

      { "_id" : "duplicate_test", "i_am_a_key" : "am i evil?", "i_am_a_key" : "yes i am!" }

      It appears that identical keys exist with conflicting values.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            stephen3000 Stephen3000
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: