Closed
Description
Versions/Environment
- What version of Rust are you using? 1.73.0
- What operating system are you using? MacOS 14.4.1 (23E224)
- What versions of the driver and its dependencies are you using?
mongodb
2.6.0 andbson
2.8.1 - What version of MongoDB are you using? 6.0.7
- What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? Replica set
Describe the bug
Collection::find_one_and_update
/ Collection::find_one_and_update_with_session
seem to be using human-readable BSON deserialization. Which results in deserialization errors, because most other methods, such as Collection::insert_one
use the non-human-readable one.
To Reproduce
- Insert a document using the following code:
fn insert_document() {
#[derive(Debug, Serialize, Deserialize)]
struct Example {
_id: ObjectId,
ip_addr: IpAddr,
}
let client =
crate::services::database::connect("mongodb://localhost:27017/?replicaSet=rs0").await;
let db = client.database("test");
let id = ObjectId::new();
db.collection("test")
.insert_one(
Example {
_id: id,
ip_addr: IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
},
None,
)
.await
.unwrap();
}
This will create a document where IpAddr
is serialized as non-human-readable, i.e. Map<String, Vec<i32>>
.
- Attempt to find and update the document:
let value = db
.collection::<Example>("test")
.find_one_and_update(
doc! { "_id": id },
doc! { "$set": { "_lock": { "seed": ObjectId::new()} } },
None,
)
.await
.unwrap();
This will result in Error { kind: BsonDeserialization(DeserializationError { message: "invalid type: map, expected IP address" }), labels: {}, wire_version: Some(17), source: None }
, because the method will attempt to deserialize IpAddr
as human-readable, i.e. String
.
- Other methods like
find_one
use correct (non-human-readable) deserialization.