Skip to content

RUST-1908 find_one_and_update / find_one_and_update_with_session use human-readable BSON deserialization #1065

Closed
@nikis05

Description

@nikis05

Versions/Environment

  1. What version of Rust are you using? 1.73.0
  2. What operating system are you using? MacOS 14.4.1 (23E224)
  3. What versions of the driver and its dependencies are you using? mongodb 2.6.0 and bson 2.8.1
  4. What version of MongoDB are you using? 6.0.7
  5. 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

  1. 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>>.

  1. 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.

  1. Other methods like find_one use correct (non-human-readable) deserialization.

Metadata

Metadata

Assignees

Labels

tracked-in-jiraTicket filed in Mongo's Jira system

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions