问题描述
我们有一个应用程序在MongoDB中存储来自C / C ++的一些配置值,并且能够重新启动(即它运行一段时间,有人中断应用程序,更改配置,然后再次运行应用程序,以及它从它停止的地方回来)。这就像布尔值和字符串配置的魅力一样。
We have an application that stores some configuration values from C/C++ in MongoDB and has the capability to be restarted (i.e. it runs for a while, someone interrupts the application, changes the configuration, then runs the app again, and it picks up where it left off). This works like a charm for boolean and string configurations.
但是我们有一些整数(在我们当前的C / C ++实现中 - 32位值)。当我使用MongoDB控制台修改这些整数值时,Mongo总是将它们存储为Number(在C / C ++实现中是可靠的)。我们将更改应用程序以获取期望整数的双值,但我想知道是否有办法强制Mongo从其JavaScript控制台存储整数。
But then we have some integers (in our current C/C++ implementation - 32 bit values). And when I use the MongoDB console to modify those integer values, Mongo always stores them back as Number (which is doulble in the C/C++ implementation). We will change the app to take double values where it expects integers but I was wondering if there is a way to force Mongo to store integers from its JavaScript console.
任何建议?
推荐答案
在C / C ++意义上,整数是为32位值。 int
必须至少为16位,但通常与平台架构匹配(例如32或64位)。
In the C/C++ "sense of the word", ints are not actually guaranteed to be 32-bit values. An int
must be at least 16-bits, but generally matches the platform architecture (eg. 32 or 64bit).
如@Jasd所述,JavaScript只有一个数字类型,它是一个浮点(C中的 double
)。
As mentioned by @Jasd, JavaScript does only have one numeric type which is a floating point (double
in C).
从MongoDB shell开始,您应该可以使用函数 NumberInt(..)
来获取或 NumberLong(..)
获取BSON 64位整数。
From the MongoDB shell you should be able to use the functions NumberInt(..)
to get a BSON 32-bit integer value or NumberLong(..)
to get a BSON 64-bit integer.
这篇关于从控制台将MongoDB字段的类型更改为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!