Arithmetic Overflow using MongoDB C# driver!

We recently resolved a crazy issue affecting one of our projects randomly…The project technology stack was MEAN stack using C# 2.6 driver for MongoDB. The issue was that “sometimes” insert/update queries wouldn’t work! On extensive logging we found the following Log in our diagnostics trace..”Arithmetic operation resulted in an overflow”. The query was an upsert query so the issue could be insert or update…there were no calculations happening in code!

A quick tip here:- You got a arithmetic operation overflow and you are doing no mathematical operation…..so most probably you are trying to insert a wrong ( rather large) value into a datatype that does not support such a value and you need to check your data types! Now the problem is that MongoDB is a flat file system and so we didn’t have data types defined per columnSmile

So our next bet was the Mongo DB C# driver! After a lot of trial and error we realized that the MongoDB driver does not have proper support for uint32 data type. It was converting unit32 into int32.

Uint Range is 0 to 4294967295 and int32 is –2,147,483,647 to +2,147,483,647 so Uint.Max is more than int32.Max. So sometimes when the value of one of the data members would become more than int32.Max, MongoDB driver would result in an overflow on execution of the upsert query!

Further googling ( yup we love google over bing…) we found another thread reporting a similar problem and a possible solution….scroll to the bottom of this Jira issue. We personally did not use this but just changed the Datatype of our object model from uint to int64 which can take values more than unit.Max!

Hope this helps someone else dealing with this crazy issue! Drop a line if it did!

Team Cennest!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>