-
Type: Improvement
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: None
-
None
-
Fully Compatible
Mongoid supports field types BSON::ObjectId and BSON::Binary per https://www.mongodb.com/docs/mongoid/master/reference/fields/#field-types, but does not support other BSON types.
In case of BSON::Int64, writes of plain Ruby integers will store them as either int32 or int64 depending on whether the value fits into int32 type, thus potentially the stored value being of incorrect type. Writes of Int64 instances will be written as int64. Data will be returned as Ruby integers regardless.
In case of BSON::Decimal128, the data will be written as int32/int64 for plain integer input and as decimal128 for BSON::Decimal128 input. With bson-ruby 4 reads will return BSON::Decimal128 if that is what was written to the database, in bson-ruby 5 reads will return BigDecimal instances regardless.
irb(main):001:0> class Post include Mongoid::Document field :f, type: BSON::Int64 field :g, type: BSON::Decimal128 end => #<Mongoid::Fields::Standard:0x00007f84cd271ce0 @default_val=nil, @label=nil, @name="g", @options={:type=>BSON::Decimal128, :klass=>Post}> irb(main):008:0> a=Post.create!(f: 1, g: 2) => #<Post _id: 628bc0a0a15d5d78eccd5340, f: 1, g: 2> irb(main):009:0> a.reload => #<Post _id: 628bc0a0a15d5d78eccd5340, f: 1, g: 2> irb(main):010:0> a.f => 1 irb(main):011:0> a.g => 2 irb(main):012:0> b=Post.create!(f: BSON::Int64.new(1), g: BSON::Decimal128.new('2')) => #<Post _id: 628bc0bfa15d5d78eccd5341, f: #<BSON::Int64:0x00007f84cd19e908 @value=1>, g: BSON::Decimal128('2')> irb(main):013:0> b.reload => #<Post _id: 628bc0bfa15d5d78eccd5341, f: 1, g: BSON::Decimal128('2')> irb(main):014:0> b.f => 1 irb(main):015:0> b.g => BSON::Decimal128('2') irb(main):016:0> a.collection.find(a: 1).first => nil irb(main):017:0> a.collection.find(f: 1).first => {"_id"=>BSON::ObjectId('628bc0a0a15d5d78eccd5340'), "f"=>1, "g"=>2} irb(main):018:0> a.collection.find(f: {'$type'=>'int64'}).first /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongo-2.17.1/lib/mongo/operation/result.rb:348:in `raise_operation_failure': [2:BadValue]: Unknown type name alias: int64 (on localhost:14400, modern retry, attempt 1) (Mongo::Error::OperationFailure) irb(main):019:0> a.collection.find(f: {'$type'=>'long'}).first => {"_id"=>BSON::ObjectId('628bc0bfa15d5d78eccd5341'), "f"=>1, "g"=>BSON::Decimal128('2')} irb(main):020:0> a.collection.find(f: {'$type'=>'int'}).first => {"_id"=>BSON::ObjectId('628bc0a0a15d5d78eccd5340'), "f"=>1, "g"=>2}
We should add a warning when parsing field types for the unsupported BSON types informing the users that the data may not be written in the declared type and may not be returned in the declared type.
Since we do not presently have a use case for returning the data as BSON types, we will not be implementing this functionality right now, but it is tracked in https://jira.mongodb.org/browse/MONGOID-5361.
Changes in scope of this ticket:
Runtime warning
Documentation note under field types
- related to
-
MONGOID-5631 Resolve Mongoid compatibility issues with bson-ruby deserializing decimal128 to BigDecimal
- Closed