Learn how to work with MongoDB’s Extended JSON (EJSON) format when using the MongoDB Connector with Ditto
MongoDB BSON | EJSON Canonical Format | Example |
---|---|---|
Date | { "$date": { "$numberLong": "<millis>" } } | { "createdAt": { "$date": { "$numberLong": "1704067200000" } } } |
ObjectId | { "$oid": "<hex string>" } | { "_id": { "$oid": "507f1f77bcf86cd799439011" } } |
NumberLong | { "$numberLong": "<string>" } | { "count": { "$numberLong": "9223372036854775807" } } |
NumberInt | { "$numberInt": "<string>" } | { "age": { "$numberInt": "42" } } |
Double | { "$numberDouble": "<string>" } | { "price": { "$numberDouble": "19.99" } } |
Decimal128 | { "$numberDecimal": "<string>" } | { "total": { "$numberDecimal": "123.456789" } } |
Binary | { "$binary": { "base64": "<base64>", "subType": "<hex>" } } | { "data": { "$binary": { "base64": "YmluYXJ5IGRhdGE=", "subType": "00" } } } |
Timestamp | { "$timestamp": { "t": <uint32>, "i": <uint32> } } | { "ts": { "$timestamp": { "t": 1704067200, "i": 1 } } } |
09/09/2001, 1:46:40 am
and 05/18/2033, 3:33:20 am
, as they fit in the range of 1000000000 to 2000000000.
If you are using dates outside of this range, you will need to use the CAST
function to convert the date to a number.
CAST
or other conversion functions) currently makes queries ineligible for index optimization in DQL. This can significantly impact query performance on large datasets.Platform | Library |
---|---|
Swift | swift-bson |
Kotlin | bson |
JavaScript/Node.js | bson |
Flutter | bson |
Java | MongoDB Java Driver |
C# | MongoDB.Bson |
Rust | bson |
CAST
) on EJSON fields cannot use indexes. This can severely impact query performance for larger datasets.Query Performance Degradation
Numeric Comparison Failures
CAST
for accurate comparisons (accepting the performance trade-off) or implement application-level filtering.Date Queries Not Working
$date.$numberLong
field and compare as milliseconds since epoch: