Operators on decimal in drivers > 2.4.0 and Mongo > 3.4

50 views
Skip to first unread message

ogborstad

unread,
Mar 8, 2017, 9:28:20 AM3/8/17
to mongodb-csharp
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 operators on them. Having to load documents into C# is a huge memory constraint that renders this useless for web scale scenarios.

Chad Kreimendahl

unread,
Jun 20, 2017, 1:51:29 PM6/20/17
to mongodb-csharp
Does anyone know if this is a database limitation or one imposed on us by the driver?

Ole Gunnar Borstad

unread,
Jun 20, 2017, 2:41:37 PM6/20/17
to mongodb...@googlegroups.com
Database limitation prior to 3.4
--
You received this message because you are subscribed to a topic in the Google Groups "mongodb-csharp" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-csharp/jbD1SoVusiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-cshar...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Craig Wilson

unread,
Jun 20, 2017, 7:22:36 PM6/20/17
to mongodb-csharp
This message is indicating that your decimal is a string in MongoDB. MongoDB can't increment a string.   This has nothing to do with not providing arithmetic operations in the driver, as this error is coming from the server.

If you used a decimal prior to upgrading to MongoDB 3.4, then by default, decimal's were stored in MongoDB as strings because it was the only way to preserve the number exactly. We can't change that default behavior just because you are talking to a 3.4 version of the server, otherwise user's applications would break. Therefore, if you want to take full advantage of the new decimal support in 3.4 of the server, you'll need to convert all your existing "decimal's stored as strings" to a MongoDB decimal.


On Wednesday, March 8, 2017 at 8:28:20 AM UTC-6, ogborstad wrote:
Reply all
Reply to author
Forward
0 new messages