SQL and data types · Engineering

Exact numbers, Decimal128, and BSON types

Why 30 equals 30.0, why large integers do not get rounded through f64, and how Decimal128 fits into one numeric bracket.

Numbers are where document databases often become quietly wrong. JavaScript loses precision above 2^53, JSON has no decimal type, and a database that normalises everything through a double will eventually turn an account balance into a surprising value.

NonoDB treats all numeric values as one MongoDB-compatible numeric bracket. JSON integers, floats, $numberInt, $numberLong, $numberDouble, and Decimal128 compare with each other by numeric value. 1, 1.0, and NumberDecimal("1.00") are equal and share the same index key.

The details still matter.

Integers are not rounded through f64

Ordinary JSON numbers are stored unchanged. Comparison and indexing are exact: integers are not rounded through f64, so values above 2^53 remain distinct.

{"_id": "a", "n": 9007199254740993}
{"_id": "b", "n": 9007199254740992}

Those two documents are not equal. A naive double conversion would collapse them.

Canonical integer forms preserve their type for $type:

{"$numberInt": "42"}
{"$numberLong": "9007199254740993"}

They still compare and index in the same numeric bracket as plain JSON numbers. The type tag is a description, not a separate arithmetic universe.

Floats use 17 significant digits

Floating-point values use 17 significant decimal digits internally for comparison and index ordering. That is enough to round-trip every finite f64 and preserve ordering.

It also means a float is still a float. If an amount must be exact to the cent, store it as Decimal128 or as an integer in minor units. Do not assume the database will make binary floating point behave like decimal arithmetic.

Decimal128

Use MongoDB Extended JSON:

{"_id": "p1", "price": {"$numberDecimal": "199.9900"}}

Decimal128 stores up to 34 significant decimal digits and the Decimal128 exponent range. Non-finite decimals are accepted and canonicalized as NaN, Infinity, or -Infinity. A decimal string in ordinary JSON is just a string:

{"price": "199.99"}

That is not a decimal. It will not compare like one.

Arithmetic

When a decimal is involved, $inc, $sum, $avg, $add, $subtract, $multiply, and $divide use exact decimal arithmetic and return a $numberDecimal.

{
"op": "update",
"coll": "accounts",
"filter": {"_id": "a"},
"update": {"$inc": {"balance": {"$numberDecimal": "0.20"}}}
}

Results are rounded to 34 significant digits with banker’s rounding and must fit Decimal128. A result outside the range fails with invalid_numeric instead of silently becoming infinity.

$inc and $mul propagate non-finite decimal results and keep the decimal type. $sum and $avg currently skip non-finite decimal inputs, matching their behavior for non-finite doubles.

NaN and Infinity

JSON cannot express these values. Use $numberDouble:

{"_id": "nan", "n": {"$numberDouble": "NaN"}}
{"_id": "inf", "n": {"$numberDouble": "Infinity"}}

Accepted strings are NaN, Infinity, -Infinity, and finite decimal strings. Sorting and index order follow:

NaN < -Infinity < negative finite < 0 < positive finite < Infinity

NaN equals only NaN. These values are stored, filtered, indexed, compared, and returned by $inc/$mul; $sum/$avg ignore non-finite inputs.

Index compatibility

Numeric index keys use an exact, order-preserving decimal encoding. Index format version 3 introduced exact numeric keys; version 4 added binary/UUID and ObjectId keys.

Older indexes are skipped for filters containing numbers, dates, binary, or ObjectId values. Queries fall back to a scan until the index is dropped and recreated. String and boolean filters can still use those older indexes. This is one reason to recreate indexes after an upgrade rather than assuming they are equivalent.

Decimal keys share the numeric type tag with other numbers. There is no separate decimal index type or collation.

Why this design

A single numeric bracket makes queries predictable: an application can write 30, 30.0, or NumberDecimal("30") and get the same comparison. Exact integer and decimal handling avoids the worst precision surprises, while floats remain available for scientific and approximate data.

The trade-off is that “number” is not one physical representation. The type is preserved for $type, and arithmetic can promote to Decimal128. If you need a strict schema, enforce it in the application or with $jsonSchema; the storage engine will not pretend a float is a decimal.

The numbers guide and BSON scalar types have the exact accepted forms and index-version details.

All engineering notes →Read the docs