Operators on decimal in drivers > 2.4.0 and Mongo > 3.4

254 views
Skip to first unread message

ogborstad

unread,
Mar 8, 2017, 9:22:20 AM3/8/17
to mongodb-user
http://mongodb.github.io/mongo-csharp-driver/2.4/what_is_new/

says that

"There are no arithmetic operations provided on Decimal128 values, but Decimal128 values can be converted to C# decimal or double values and you can operate on those."

Is this why I am seing error messages such as

"A write operation resulted in an error.
  Cannot apply $inc to a value of non-numeric type. {_id: ObjectId('58c011a05aca4557e0f6b64b')} has the field 'Amount' of non-numeric type string"

when doing

var writeModelUpdate = new UpdateOneModel<BsonDocument>(
    Builders<BsonDocument>.Filter.Eq(y => y["_id"], id),
    Builders<BsonDocument>.Update
       .Inc(y => y["Amount"], 1m)
 );

It seems to me the C# support for decimal is quite useless if I cant perform database operations on them. Having to load documents into C# is a huge memory constraint that renders this useless for web scale scenarios.

Robert Stam

unread,
Mar 8, 2017, 9:44:13 AM3/8/17
to mongod...@googlegroups.com
The C# driver supported serializing the .NET Decimal type long before the server added a Decimal128 data type. In order to do so Decimal values were serialized as strings.

So for example, the following class:

public class C
{
    public int Id { get; set; }
    public decimal D { get; set; }
}

Would be serialized as:

{ _id : 1, D : "1.5" }

When you read this back in your C# program the string will be parsed back into a .NET Decimal value. But server side it is a string, which is why you are seeing that server side error message.

For backward compatibility reasons the C# driver still (by default) serializes .NET Decimal values as strings. You can opt in to serializing .NET Decimal values as BSON Decimal128 values like this:

public class C
{
    public int Id { get; set; }
    [BsonRepresentation(BsonType.Decimal128)]
    public decimal D { get; set; }
}

Finally, be aware that the .NET Decimal type and the BSON Decimal128 type are not exactly the same. The range of BSON Decimal128 is much larger than the range of .NET Decimal. That means that all .NET Decimal values can be safely stored in a BSON Decimal128 field, but not all BSON Decimal128 values can be converted to .NET Decimal values.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/a94bd13c-c9e6-43e7-be14-f35a87219f81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ole Gunnar Borstad

unread,
Mar 8, 2017, 12:25:23 PM3/8/17
to mongod...@googlegroups.com
Thanks. Awesome answer!
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/N7Hjzi8exlU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages