日期:2014-05-16  浏览次数:20506 次

利用ruby 向 mongodb 插入 二进制内容(图像文件)
require 'mongo'
require "fileutils"

@conn = Mongo::Connection.new("localhost", 30000)
@conn.database_names.each { |name| puts name }
@db = @conn['sample_db']
@coll = @db['test']

doc = {"name" => "MongoDB", "type" => "database", "count" => 1,
       "info" => {"x" => 203, "y" => '102'}}
       
#@coll.insert(doc)

File.open("E:\\eng_00\\1.jpg", 'rb') {|fin|
    fileText = fin.read
    10000.times do |i|
      puts(i)
      @coll.insert({'LI' => i+30000, 'G' => BSON::Binary.new(fileText)})
     end 
}
      


puts "There are #{@coll.count} records. Here they are:" 
@coll.find.each { |doc| puts doc.inspect }