Search code examples
jsonmongodbrustbson

is there a way to quickly convert Json<Value> to bson to be able to save it to mongo?


I have Json<Value> object (from serde_json::Value) with no Rust struct (because objects have quite many properties and it may change) and I would like to convert it to mongodb::Document or bson to save it to mongo, is there any integration or should I write transformer myself


Solution

  • serde_json::Value implements Serialize so just use bson::to_bson, or bson::to_document

    let value: serde_json::Value = serde_json::json!({"foo": "bar"});
    let bson_value = bson::to_bson(&value);