Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MongoDB support floating point types?

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?

like image 274
ulima69 Avatar asked Oct 07 '11 03:10

ulima69


People also ask

Does MongoDB support float?

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 .

What is double data type in MongoDB?

Double: The double data type is used to store the floating-point values. Example: In the following example we are storing the marks of the student in the student collection: 4. Boolean: The boolean data type is used to store either true or false.

Which format supports large data types in MongoDB?

MongoDB BSON provides support for additional data types than JSON.


3 Answers

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.

like image 123
Chris Fulstow Avatar answered Oct 08 '22 08:10

Chris Fulstow


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

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

like image 9
Salvador Dali Avatar answered Oct 08 '22 09:10

Salvador Dali


It has, Introduction

A value is a

  • basic type like string, integer, float, timestamp, binary, etc.,
  • a document, or
  • an array of values
like image 1
lostyzd Avatar answered Oct 08 '22 08:10

lostyzd