database MongoDB 是否支持浮点类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7682714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:20:51  来源:igfitidea点击:

Does MongoDB support floating point types?

mongodbdatabasenosql

提问by ulima69

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 模式中有一些带有十进制类型的字段,我该怎么办或我能做什么?

回答by Chris Fulstow

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

MongoDB 以称为BSON的二进制格式存储数据,该格式支持以下数字数据类型:

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

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

decimalMongoDB 中没有与 mySQL类型等效的精确定点值,但您可以在 Mongo 中将 64 位浮点数存储为double.

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 语言驱动程序会区分整数和浮点类型。

回答by Salvador Dali

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

Chris 已经提供了有关浮点类型的信息,因此我将添加有关Mongo 3.4 中添加的Decimal 数据类型的信息

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.

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...

3.4 使用新的十进制数据类型添加了对 decimal128 格式的支持。decimal128 格式支持最多 34 位十进制数字(即有效数字)和指数范围为 ?6143 到 +6144 的数字。

与仅存储十进制值的近似值的双精度数据类型不同,十进制数据类型存储精确值。例如,十进制 NumberDecimal("9.99") 的精确值为 9.99,而双精度 9.99 的近似值为 9.9900000000000002131628...

回答by lostyzd

It has, Introduction

它有,介绍

A value is a

  • basic type like string, integer, float, timestamp, binary, etc.,
  • a document, or
  • an array of values

一个值是一个

  • 基本类型,如字符串、整数、浮点数、时间戳、二进制等,
  • 一份文件,或
  • 一组值

回答by duskwuff -inactive-

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+Types+and+Conventions-

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

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