title: How to convert mongodb::Bson::Document to byte array (Vec<u8>) in Rust
body: <p>Im trying to send Bson Objects via Websocktes between c# and rust using this code to convert byte[] into mongodb::bson::Document in Rust.</p>
  <pre><code>if msg.is_binary() {
     let mut bin = std:::io::Cursor::new(msg.into_data());
     let doc = mongodb::bson::Document::from_reader(&amp;mut bin); 
  }
  </code></pre>
  <p>That works for a byte[] coming from c# MongoDB.Bson.ToBson()</p>
  <p>but I wonder how to do the opposite.
  <strong>How would i convert mongodb::bson::Document into Vec</strong> byte array, so that i can parse it for example with: BsonDocument.ReadFrom() in c#.</p>
  <p>Answer is:</p>
  <pre><code>let doc = doc! {&quot;key&quot;: &quot;value&quot;};
  let mut byte_array : Vec&lt;u8&gt; = vec![];
  doc.to_writer(&amp;mut byte_array);
  /// byte_array is your BsonDocument as Vec&lt;u8&gt;
  </code></pre>