MongoDB 是否支持浮点类型? [英] Does MongoDB support floating point types?

查看:65
本文介绍了MongoDB 是否支持浮点类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 mysql 数据库迁移到 mongodb.但我读过 MongoDb 数据类型,然后没有引用浮点类型,如浮点、双精度、小数.

以及如何在我的 sql 架构中有一些具有十进制类型的字段,我该怎么做或我能做什么?

解决方案

MongoDB 以一种名为 BSON 的二进制格式存储数据,它支持这些数字数据类型:

  • int32 - 4 字节(32 位有符号整数)
  • int64 - 8 个字节(64 位有符号整数)
  • double - 8 字节(64 位 IEEE 754 浮点)

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

值得一提的是,MongoDB shell - 作为一个 JavaScript shell - 不识别整数和浮点值之间的区别,它对待所有数字都是一样的,因为 JavaScript 将所有数字表示为 64 位浮点,不管它们的底层 BSON 类型.

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

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.

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

解决方案

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

  • int32 - 4 bytes (32-bit signed integer)
  • int64 - 8 bytes (64-bit signed integer)
  • double - 8 bytes (64-bit IEEE 754 floating point)

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.

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.

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

这篇关于MongoDB 是否支持浮点类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆