MongoDB Java development data type conversion exception caused by reading Object value from Mongo to Long exception

    Recently, in an automated operation and maintenance platform, the monitoring data collection frequency was modified through the front page page. As a result, the platform monitoring was no longer refreshed. Observe the background log and find

The error of data type conversion exception has been brushed.

     Development language: JAVA

    JDK version: java version "1.8.0_211"

    MongoDB version: 3.2.16

    The background log reports errors as follows:

    According to the java class of the error prompt, the specific error code is as follows:

    From this, it can be seen that the code that caused the exception is:

long intervalDynamic = conf.getLong("interval_dynamic");

    Observe the corresponding data in MongoDB:

    Up to this point, the problem may lie in the data type conversion exception when the program converts the interval_dynamic in the mongodb database from Object to Long.

The initialization of the communication monitoring platform is the background script initialization. When mongdb is initialized, it is a number type. When the monitoring is taken out, it is bson. The value is set by key.

The Object type cannot be directly cast to Long, which may be related to the JDK version.

    After debugging, the data conversion is adjusted as follows, and the program resumes normal operation:

    Summary: JAVA reads MongoDB data as binary Bson, and the data obtained by JAVA by field is Object data. Before converting to Long, go through

Transform to the String type, and then use the Long.valueOf(String) method to convert the String data to the Long type.

 

Guess you like

Origin blog.csdn.net/www_xue_xi/article/details/107681726