-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 2.0.4
-
Component/s: GridFS
-
None
-
Environment:Ruby on rails
I used below code to save a excel file into Mongo but unfortunately I got the encoding error:
require 'mongo'
MONGODB_URL = 'mongodb://localhost:27017/'
DEFAULT_DATABASE = 'game_of_threats_development'
@db = nil
@grid = nil
begin
@db ||= Mongo::Client.new(MONGODB_URL, :database => DEFAULT_DATABASE).database
rescue Errno::ECONNRESET => e
puts e.backtrace
end
data = File.open 'test.xls'
grid_file = Mongo::Grid::File.new(data.read, :filename => 'test.xls')
result = @db.fs.insert_one(grid_file)
The error trace I got are:
/Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/binary.rb:137:in `block in to_bson': incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/encodable.rb:81:in `encode_binary_data_with_placeholder'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/binary.rb:134:in `to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:46:in `block (2 levels) in to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:43:in `each'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:43:in `block in to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:42:in `to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/grid/file/chunk.rb:137:in `to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/array.rb:49:in `block (2 levels) in to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/array.rb:46:in `each'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/array.rb:46:in `each_with_index'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/array.rb:46:in `block in to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/array.rb:45:in `to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:46:in `block (2 levels) in to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:43:in `each'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:43:in `block in to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/bson-3.0.3/lib/bson/hash.rb:42:in `to_bson'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/protocol/serializers.rb:155:in `serialize'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:153:in `block in serialize_fields'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:141:in `each'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:141:in `serialize_fields'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:70:in `serialize'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/connection.rb:123:in `block in write'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/connection.rb:122:in `each'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/connection.rb:122:in `write'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/connectable.rb:66:in `block in dispatch'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/loggable.rb:44:in `log'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/loggable.rb:65:in `log_debug'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/connectable.rb:65:in `dispatch'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/operation/executable.rb:35:in `block in execute'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/connection_pool.rb:99:in `with_connection'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/server/context.rb:63:in `with_connection'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/operation/executable.rb:34:in `execute'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/operation/write/insert.rb:71:in `execute_write_command'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/operation/write/insert.rb:61:in `execute'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/collection.rb:196:in `insert_many'
from /Users/jbao009/Documents/gitbase/Game-of-Threats/vendor/ruby/2.1.0/gems/mongo-2.0.4/lib/mongo/grid/fs.rb:78:in `insert_one'
from test.rb:18:in `<main>'