MongoDB是否支持浮点类型?

时间:2022-03-07 16:29:19

I am migrating a mysql database to mongodb. But i have read in MongoDb data types and then there is no reference to floating point types like, float, double, decimal.

我正在将mysql数据库迁移到mongodb。但是我已经读过MongoDb数据类型,并且没有对浮点类型的引用,比如float、double、decimal。

And how i have some fields with decimal types in my sql schema , how can i do or what can i do?

以及如何在sql模式中使用具有十进制类型的字段,如何操作或如何操作?

5 个解决方案

#1


28  

MongoDB stores data in a binary format called BSON which supports these numeric data types:

MongoDB以二进制格式存储数据,BSON支持这些数字数据类型:

  • int32 - 4 bytes (32-bit signed integer)
  • int32 - 4字节(32位带符号整数)
  • int64 - 8 bytes (64-bit signed integer)
  • int64 - 8字节(64位有符号整数)
  • double - 8 bytes (64-bit IEEE 754 floating point)
  • 双- 8字节(64位IEEE 754浮点数)

There's no exact value fixed-point equivalent to mySQL's decimal type in MongoDB, but you can store 64-bit floating point numbers in Mongo as a double.

在MongoDB中没有与mySQL的十进制类型等价的精确值定点,但是可以将64位浮点数作为双精度存储在Mongo中。

It's worth mentioning that the MongoDB shell - being a JavaScript shell - doesn't recognise the distinction between integer and floating-point values, it treats all numbers the same because JavaScript represents all numbers as 64-bit floating point, regardless of their underlying BSON type.

值得一提的是,MongoDB shell是一个JavaScript shell,它没有识别整数和浮点值之间的区别,它对所有数字都一视同仁,因为JavaScript将所有数字表示为64位浮点数,而不考虑它们的底层BSON类型。

Most MongoDB language drivers, however, make the distinction between integer and floating point types.

然而,大多数MongoDB语言驱动程序区分整数类型和浮点类型。

#2


3  

Chris already provided information about floating point types, so I will just add information about Decimal data type that was added in Mongo 3.4

Chris已经提供了关于浮点类型的信息,所以我将只添加关于在Mongo 3.4中添加的十进制数据类型的信息

3.4 adds support for the decimal128 format with the new decimal data type. The decimal128 format supports numbers with up to 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144.

3.4使用新的十进制数据类型增加对decimal128格式的支持。decimal128格式支持数字高达34小数位数(即有效数字)和一个指数+ 6144−6143范围。

Unlike the double data type, which only stores an approximation of the decimal values, the decimal data type stores the exact value. For example, a decimal NumberDecimal("9.99") has a precise value of 9.99 where as a double 9.99 would have an approximate value of 9.9900000000000002131628...

与只存储十进制值近似值的双数据类型不同,十进制数据类型存储精确值。例如,十进制数字(“9.99”)的精确值是9.99,而双9.99的精确值是9.9900000000000002131628。

#3


1  

It has, Introduction

它的介绍

A value is a

一个值是一个

  • basic type like string, integer, float, timestamp, binary, etc.,
  • 基本类型,如字符串、整数、浮点数、时间戳、二进制等。
  • a document, or
  • 一个文档,或者
  • an array of values
  • 一组值

#4


0  

MongoDB does support storing doubles, but not all drivers provide an interface to them for some reason.

MongoDB支持存储双打,但并不是所有的驱动程序都为它们提供了接口。

From http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions -

从http://www.mongodb.org/display/DOCS/Data + +和+约定-类型

Mongo uses special data types in addition to the basic JSON types of string, integer, boolean, double, null, array, and object.

Mongo除了基本JSON类型的字符串、整数、布尔、双、空、数组和对象之外,还使用特殊的数据类型。

#5


0  

Actually every number in MongoDB is a 64-bit floating point number. The problem arises from the Javascript shell not being able to represent some floating point values. You shouldn't have any problem converting your float, doubles or decimals into numbers in Mongo.

实际上MongoDB中的每个数字都是一个64位浮点数。问题来自于Javascript shell不能表示一些浮点值。你不应该有任何问题把你的浮动,双数或小数转换成数字在蒙戈。

#1


28  

MongoDB stores data in a binary format called BSON which supports these numeric data types:

MongoDB以二进制格式存储数据,BSON支持这些数字数据类型:

  • int32 - 4 bytes (32-bit signed integer)
  • int32 - 4字节(32位带符号整数)
  • int64 - 8 bytes (64-bit signed integer)
  • int64 - 8字节(64位有符号整数)
  • double - 8 bytes (64-bit IEEE 754 floating point)
  • 双- 8字节(64位IEEE 754浮点数)

There's no exact value fixed-point equivalent to mySQL's decimal type in MongoDB, but you can store 64-bit floating point numbers in Mongo as a double.

在MongoDB中没有与mySQL的十进制类型等价的精确值定点,但是可以将64位浮点数作为双精度存储在Mongo中。

It's worth mentioning that the MongoDB shell - being a JavaScript shell - doesn't recognise the distinction between integer and floating-point values, it treats all numbers the same because JavaScript represents all numbers as 64-bit floating point, regardless of their underlying BSON type.

值得一提的是,MongoDB shell是一个JavaScript shell,它没有识别整数和浮点值之间的区别,它对所有数字都一视同仁,因为JavaScript将所有数字表示为64位浮点数,而不考虑它们的底层BSON类型。

Most MongoDB language drivers, however, make the distinction between integer and floating point types.

然而,大多数MongoDB语言驱动程序区分整数类型和浮点类型。

#2


3  

Chris already provided information about floating point types, so I will just add information about Decimal data type that was added in Mongo 3.4

Chris已经提供了关于浮点类型的信息,所以我将只添加关于在Mongo 3.4中添加的十进制数据类型的信息

3.4 adds support for the decimal128 format with the new decimal data type. The decimal128 format supports numbers with up to 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144.

3.4使用新的十进制数据类型增加对decimal128格式的支持。decimal128格式支持数字高达34小数位数(即有效数字)和一个指数+ 6144−6143范围。

Unlike the double data type, which only stores an approximation of the decimal values, the decimal data type stores the exact value. For example, a decimal NumberDecimal("9.99") has a precise value of 9.99 where as a double 9.99 would have an approximate value of 9.9900000000000002131628...

与只存储十进制值近似值的双数据类型不同,十进制数据类型存储精确值。例如,十进制数字(“9.99”)的精确值是9.99,而双9.99的精确值是9.9900000000000002131628。

#3


1  

It has, Introduction

它的介绍

A value is a

一个值是一个

  • basic type like string, integer, float, timestamp, binary, etc.,
  • 基本类型,如字符串、整数、浮点数、时间戳、二进制等。
  • a document, or
  • 一个文档,或者
  • an array of values
  • 一组值

#4


0  

MongoDB does support storing doubles, but not all drivers provide an interface to them for some reason.

MongoDB支持存储双打,但并不是所有的驱动程序都为它们提供了接口。

From http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions -

从http://www.mongodb.org/display/DOCS/Data + +和+约定-类型

Mongo uses special data types in addition to the basic JSON types of string, integer, boolean, double, null, array, and object.

Mongo除了基本JSON类型的字符串、整数、布尔、双、空、数组和对象之外,还使用特殊的数据类型。

#5


0  

Actually every number in MongoDB is a 64-bit floating point number. The problem arises from the Javascript shell not being able to represent some floating point values. You shouldn't have any problem converting your float, doubles or decimals into numbers in Mongo.

实际上MongoDB中的每个数字都是一个64位浮点数。问题来自于Javascript shell不能表示一些浮点值。你不应该有任何问题把你的浮动,双数或小数转换成数字在蒙戈。