Skip to content

Decimal128 feature not storing the right value to MongoDB #282

Closed
@ale-rinaldi

Description

@ale-rinaldi

Hello,

I'm having a fairly serious issue in using the decimal128 feature, with bson 2.0.0-beta.2.

I created a really simple, compilable project that describes the issue: https://github.com/ale-rinaldi/rust-decimal-test

The important part is here:

/* uses */

#[derive(Serialize)]
struct TestData {
    pub(crate) my_date: bson::DateTime,
    pub(crate) my_decimal: bson::Decimal128,
    pub(crate) my_string: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    /* Code for connection */

    let data = TestData {
        my_date: bson::DateTime::from_millis(1278720000000),
        my_decimal: bson::Decimal128::from_u32(42),
        my_string: "foobar".to_string()
    };
    let doc = bson::to_document(&data).unwrap();
    collection.insert_one(doc, None).await.unwrap();
    Ok(())
}

So I'm creating a simple test struct with a Date, a Decimal128 and a String, and saving it to Mongo (I'm on MongoDB 4.4.7 but I tried this with various versions, even back to 4.1.13, and the issue is the same).
What happens here is that, if I look at the created document using MongoDB Compass, the date and the string fields have the value that I except (2010-07-10T00:00:00.000+00:00 and foobar), while the decimal has no value 42 but 6.6E-1819.

The strange thing is that, if I read the document back from Rust, the value of the returned Decimal128 is now correct (42).

Am I totally missing the point here, or is it an actual problem with the bson serializer?

Thanks

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions