"文件超出允许的最大BSON大小。最大值是16777216。"使用GridFS

时间:2015-05-21 09:20:13

标签: ruby mongodb gridfs

我正在尝试使用GridFS和ruby在我的mongo数据库中插入一个33 MB的视频文件,我有一个系统的"文档超过允许的最大BSON大小。最大值是16777216。"。 我认为在mongo集合中插入大于16 MB的文件的唯一方法是使用Gridfs,所以我怀疑我做错了,即使我复制/粘贴Ruby驱动程序示例(http://docs.mongodb.org/ecosystem/tutorial/ruby-driver-tutorial/#gridfs)。

我使用的是Ruby 2.2.1,mongo驱动程序2.0.4和mongo db 3.0.1。 我的代码:

eDatabase = Mongo::Client.new(......)
@grid = @eDatabase.database.fs


myvideo = File.open("files/33mo.mov", 'rb')

grid_file = Mongo::Grid::File.new(myvideo.read, :filename => "myvideo.mov")
@grid.insert_one(grid_file)

我的日志:

21/05/2015 11:12:25] ERROR : Exception in main loop : Document exceeds allowed max BSON size. The max is 16777216.
[21/05/2015 11:12:25] ERROR : Backtrace : /Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/protocol/serializers.rb:157:in `serialize'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:153:in `block in serialize_fields'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:141:in `each'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:141:in `serialize_fields'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/protocol/message.rb:70:in `serialize'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/connection.rb:123:in `block in write'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/connection.rb:122:in `each'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/connection.rb:122:in `write'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/connectable.rb:66:in `block in dispatch'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/loggable.rb:44:in `log'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/loggable.rb:65:in `log_debug'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/connectable.rb:65:in `dispatch'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/operation/executable.rb:35:in `block in execute'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/connection_pool.rb:99:in `with_connection'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/server/context.rb:63:in `with_connection'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/operation/executable.rb:34:in `execute'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/operation/write/insert.rb:71:in `execute_write_command'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/operation/write/insert.rb:61:in `execute'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/collection.rb:196:in `insert_many'
/Users/Atome/.rvm/gems/ruby-2.2.1/gems/mongo-2.0.4/lib/mongo/grid/fs.rb:78:in `insert_one'
server.rb:530:in `handle_addobject_message'

仅供参考,line server.rb:530对应

@grid.insert_one(grid_file)

注意:

  • 完全相同的代码与< 16 MB文件,即插入机会和文件文件
  • 当我使用mongofiles命令时,一切正常......
  • 数据库中的其他插入可以从我的应用程序中正常工作

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

根据mongodb jira ticket RUBY-946,这是一个错误,应该在新发布的Ruby驱动程序2.0.5中得到纠正。 请参阅this google group

中的评论