MongoDB's Truncationexception exception resolution method

Source: Internet
Author: User
Tags serialization

Recently due to some of the db4o bug, resulting in the current project query efficiency under, so angry and migrated to MongoDB.

MongoDB Although more than db4o users, but the document is still extremely scarce, the problem is not so easy to search for a solution, here to share a more deadly anomaly problems encountered in the correction method.

Description of the exception

My Project uses the C # version of the official driver, has been working well, today during the visit suddenly there is such an exception, and no matter how the refresh can not be restored:

MongoDB.Bson.TruncationException:Truncation resulted in data loss.

[truncationexception:truncation resulted in data loss.]
   MongoDB.Bson.Serialization.Options.RepresentationSerializationOptions.ToSingle (Double value) + 339
   MongoDB.Bson.Serialization.Serializers.SingleSerializer.Deserialize (Bsonreader bsonreader, Type Nominaltype, type Actualtype, ibsonserializationoptions options) +257
   MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue (Bsonreader Bsonreader, Bsonmembermap MEMBERMAP) +342

[Fileformatexception:an error occurred while deserializing the Money property of class MONGOMODELS.USER: Truncation resulted in data loss.
MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue (Bsonreader Bsonreader, Bsonmembermap MEMBERMAP) +878
MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (Bsonreader bsonreader, type Nominaltype, type Actualtype, ibsonserializationoptions options) +1343
MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (Bsonreader bsonreader, Type Nominaltype, Ibsonserializationoptions options) +247

Any page that reads the user's data will report this error, completely unable to use the site properly.

Cause analysis

Note the Red content location that I marked in the error prompt, and the problem can be anchored to the money property of the user class:

 Public float Money        {            get;             Set ;        }

This property is very simple, is a float type of number, how can it be wrong?

Using Mongovue to view the user collection of the database is completely normal, except that the money value is a floating-point number, there will be some error, resulting in a very long scale.

Direct search for "truncation resulted in data loss" without fruit, and found a similar problem answer:

http://stackoverflow.com/questions/5314238/ how-do-i-set-the-serialization-options-for-the-geo-values-using-the-official-10g

Follow the methods provided by the solution to try it out:

true )]        publicfloat  money        {            get;             Set ;        }

Sure enough, it seems that the main thing to explicitly declare is to allow the driver to truncate the number of extra bits of a floating-point type.

Summarize

This is really pure pit father design, early not mandatory declaration, the operation of the need to truncate when the error, time bomb as ferocious.

It seems that floating-point data like decimal, float, double should be explicitly declared, otherwise it will leave a serious risk.

MongoDB's Truncationexception exception resolution method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.