ruby:BSON序列化和反序列化
require 'bson' doc = { _id: BSON::ObjectId.new, username:"kbanker", action_code:rand(5), time:Time.now.utc, n:1 } bson = doc.to_bson puts "Document #{doc.inspect} takes up #{bson.length} bytes as BSON" deserialized_doc = BSON::Document.from_bson(bson); puts "Here's our document deserialized from BSON:" puts deserialized_doc.inspect
注意:Ruby散列的值必须能转换为BSON类型,键名必须合法(由null结尾的字符串组成;最大长度为255字节;字符串不能以$开头,不能包含.字符,除结尾处外不能包含null字节)。