且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB:类 java.lang.Double 不能转换为类 java.lang.Long

更新时间:2023-01-17 22:29:53

问题可能是任何指示的字段实际上存储为 Double 而不是 Long.

The problem probably is that any of the indicated fields is actually stored as Double instead of Long.

正如您在 源代码Document类,在getLong方法中可以找到:

As you can see in the source code of the Document class, in the getLong method you can find the following:

return (Long) get(key);

此演员是导致 Mongo 驱动程序报告错误的演员.

This cast is the one that is causing the error reported by the Mongo driver.

您的某些时间戳是否可能是使用 Javascript 和 NodeJS 创建的,也许是使用 Mongo shell 创建的?如果是这种情况,根据您创建时间戳的方式,由于数字在 Javascript 中的表示方式,您的字段值可能会插入为 double.

Is it possible that some of your timestamps are created with Javascript and NodeJS, perhaps with the Mongo shell? If that is the case, depending on how you create the timestamp, the value of your field could be inserted as double due to the way numbers are represented in Javascript.

mongo shell 文档说明:

The mongo shell documentation indicates:

默认情况下,mongo shell 将所有数字视为浮点值.

The mongo shell treats all numbers as floating-point values by default.

:

默认情况下,mongo shell 将所有数字视为 64 位浮点 double 值.

The mongo shell treats all numbers as 64-bit floating-point double values by default.

此问题已在此处记录在此 问题.它已经过时了,但可以解释您的问题.

The issue has been documented here in SO in this question. It is quite dated, but it could be a possible explanation of your problem.

这篇文章,虽然也很过时与您的问题没有直接关系,提供一些额外的背景.

This article, although also quite dated and not directly related with your problem, provides some additional background.