Repository: mongodb/mongo-rust-driver (language: rust)

Issue:
Replacements in collections use different serializer than insertions
<!--
  Make sure you have read CONTRIBUTING.md completely before you file a new
  issue!
-->

## Versions/Environment
1. What version of Rust are you using?
   ```
   cargo 1.68.2 (6feb7c9cf 2023-03-26)
   ```
3. What operating system are you using?
   ```
   Linux 6.4.3-arch1-2 x86_64 GNU/Linux
   ```
4. What versions of the driver and its dependencies are you using? (Run `cargo pkgid mongodb` & `cargo pkgid bson`)
   ```
   https://github.com/rust-lang/crates.io-index#mongodb@2.6.0
   https://github.com/rust-lang/crates.io-index#bson@2.6.1
   ```
5. What version of MongoDB are you using? (Check with the MongoDB shell using `db.version()`)
   ```
   6.0
   ```
6. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
   ```
   replica set
   ```


## Describe the bug

For types that use different a serializer for human-readable serialization, we face the following issue:

When inserted, values are serialized by the non-human readable serializer on account of using `bson::to_vec` under the hood.

However, if we try to replace such values all `replace` methods in [Collection][] use internally `bson::to_document` that uses the human-readable serializer.

This implies that if we attempt to find such a value that has been replaced and deserialize, we will either get a deserialization error, or incorrect data.

**To Reproduce**
Steps to reproduce the behavior:
1. Define a type that implements `serde_with::SerializeAs` using different serializers for human-readable (e.g. `String`) and non-readable (e.g.  `Bytes`).
2. Then insert a value of this type into a collection. Store it as `original`
4. After doing that, replace that value with a clone of itself using any of the [Collection] methods that replace values (e.g. `replace_one`).
5. Get the replaced value `replaced` from the database and verify that `!assert_eq!(original, replaced)`.

[Collection]: https://docs.rs/mongodb/latest/mongodb/struct.Collection.html



Discussion / hints:

Fix patch:
diff --git a/src/coll/mod.rs b/src/coll/mod.rs
index a07d2bac8..4ccaf3e87 100644
--- a/src/coll/mod.rs
+++ b/src/coll/mod.rs
@@ -15,7 +15,7 @@ use serde::{
 
 use self::options::*;
 use crate::{
-    bson::{doc, to_document, Bson, Document},
+    bson::{doc, to_document_with_options, Bson, Document, SerializerOptions},
     bson_util,
     change_stream::{
         event::ChangeStreamEvent,
@@ -1119,7 +1119,10 @@ where
         options: impl Into<Option<FindOneAndReplaceOptions>>,
         session: impl Into<Option<&mut ClientSession>>,
     ) -> Result<Option<T>> {
-        let replacement = to_document(replacement.borrow())?;
+        let replacement = to_document_with_options(
+            replacement.borrow(),
+            SerializerOptions::builder().human_readable(false).build(),
+        )?;
 
         let session = session.into();
 
@@ -1379,7 +1382,10 @@ where
         options: impl Into<Option<ReplaceOptions>>,
         session: impl Into<Option<&mut ClientSession>>,
     ) -> Result<UpdateResult> {
-        let replacement = to_document(replacement.borrow())?;
+        let replacement = to_document_with_options(
+            replacement.borrow(),
+            SerializerOptions::builder().human_readable(false).build(),
+        )?;
 
         bson_util::replacement_document_check(&replacement)?;
 


Test patch: