I'm trying to save a rust_decimal::Decimal
object in MongoDB.
My first attempt was generally:
// self.db is of type mongodb::sync::Database
decimal = Decimal::new(1, 20);
let order = doc! {"decimal": decimal};
self.db.collection("collection").insert_one(order, None).unwrap();
Which yielded the error: the trait 'From<Decimal>' is not implemented for 'Bson'
This led me to learn about the bson
crate. In its documentation I found about Decimal128
, which Mongo supports, but it doesn't seem to have a convenient way to convert from either Decimal
or String
or anything really.
How do I insert the Decimal
object to the document?
Crates versions:
mongodb 2.2.2
bson 2.3.0
rust_decimal 1.24.0
Edit: I've tried Decimal128::from_bytes(decimal.serialize())
but it turns 1 in Decimal to 4.294967296E-6167 in Decimal128 (little/big endian issue?).