33 releases

✓ Uses Rust 2018 edition

0.14.1 Mar 17, 2020
0.14.0 Aug 31, 2019
0.13.1 Aug 28, 2019
0.13.0 Aug 11, 2018
0.1.2 Jun 24, 2015
Download history 5197/week @ 2020-01-25 4375/week @ 2020-02-01 4428/week @ 2020-02-08 5360/week @ 2020-02-15 5174/week @ 2020-02-22 6185/week @ 2020-02-29 7072/week @ 2020-03-07 5932/week @ 2020-03-14 5144/week @ 2020-03-21 6633/week @ 2020-03-28 7509/week @ 2020-04-04 6307/week @ 2020-04-11 6574/week @ 2020-04-18 7728/week @ 2020-04-25 6262/week @ 2020-05-02 6209/week @ 2020-05-09

24,611 downloads per month
Used in 59 crates (42 directly)

MIT license

3.5K SLoC

Ruby HTML 3.5K SLoC // 0.1% comments Scons 21 SLoC // 0.2% comments


Build Status crates.io crates.io dependency status

Encoding and decoding support for BSON in Rust

Useful links


This crate works with Cargo and can be found on crates.io with a Cargo.toml like:

bson = "0.14"


Prepare your struct for Serde serialization:

#[derive(Serialize, Deserialize, Debug)]
pub struct Person {
    #[serde(rename = "_id")]  // Use MongoDB's special primary key field name when serializing 
    pub id: bson::oid::ObjectId,
    pub name: String,
    pub age: i32

Serialize the struct:

use bson;

let person = Person {
    id: "12345",
    name: "Emma",
    age: 3

let serialized_person = bson::to_bson(&person)?;  // Serialize

if let bson::Bson::Document(document) = serialized_person {
    mongoCollection.insert_one(document, None)?;  // Insert into a MongoDB collection
} else {
    println!("Error converting the BSON object into a MongoDB document");

Deserialize the struct:

use bson::doc;

// Read the document from a MongoDB collection
let person_document = mongoCollection.find_one(Some(doc! { "_id":  bson::oid::ObjectId::with_string("12345").expect("Id not valid") }), None)?
    .expect("Document not found");

// Deserialize the document into a Person instance
let person = bson::from_bson(bson::Bson::Document(person_document))?

Breaking Changes

In the BSON specification, unsigned integer types are unsupported; for example, u32. In the older version of this crate (< v0.8.0), if you uses serde to serialize unsigned integer types into BSON, it will store them with Bson::FloatingPoint type. From v0.8.0, we removed this behavior and simply returned an error when you want to serialize unsigned integer types to BSON. #72

For backward compatibility, we've provided a mod bson::compat::u2f to explicitly serialize unsigned integer types into BSON's floating point value as follows:

fn test_compat_u2f() {
    #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
    struct Foo {
        #[serde(with = "bson::compat::u2f")]
        x: u32

    let foo = Foo { x: 20 };
    let b = bson::to_bson(&foo).unwrap();
    assert_eq!(b, Bson::Document(doc! { "x": Bson::FloatingPoint(20.0) }));

    let de_foo = bson::from_bson::<Foo>(b).unwrap();
    assert_eq!(de_foo, foo);

In this example, we added an attribute #[serde(with = "bson::compat::u2f")] on field x, which will tell serde to use the bson::compat::u2f::serialize and bson::compat::u2f::deserialize methods to process this field.


We encourage and would happily accept contributions in the form of GitHub pull requests. Before opening one, be sure to run the tests locally; check out the testing section for information on how to do that. Once you open a pull request, your branch will be run against the same testing matrix that we use for our continuous integration system, so it is usually sufficient to only run the integration tests locally against a standalone. Remember to always run the linter tests before opening a pull request.

Running the tests

Integration and unit tests

To actually run the tests, you can use cargo like you would in any other crate:

cargo test --verbose # runs against localhost:27017

Linter Tests

Our linter tests use the nightly version of rustfmt to verify that the source is formatted properly and the stable version of clippy to statically detect any common mistakes. You can use rustup to install them both:

rustup component add clippy --toolchain stable
rustup component add rustfmt --toolchain nightly

To run the linter tests, run the check-clippy.sh and check-rustfmt.sh scripts in the .evergreen directory:

bash .evergreen/check-clippy.sh && bash .evergreen/check-rustfmt.sh

Continuous Integration

Commits to master are run automatically on evergreen.


~49K SLoC