0
votes

As the title states, here is the following code.

    let users_coll = db
        .database("foo")
        .collection::<bson::oid::ObjectId>("users");
    let user_id = users_coll
        .find_one(
            doc! { "email": &account.email },
            mongodb::options::FindOneOptions::builder()
                .projection(doc! { "_id": 1i32 })
                .build(),
        )
        .await?
        .unwrap();

But it fails at ? operator with the following mongodb::error::Error,

Error { kind: BsonDeserialization(DeserializationError { message: "expected map containing extended-JSON formatted ObjectId, instead found { \"_id\": ObjectId(\"62af199df4a16d3ea6056536\") }" }), labels: {}, wire_version: None, source: None }

And it is right. Given ObjectId should be in this format,

{
    "_id": {
        "$oid": "62af199df4a16d3ea6056536"
    }
}

But I do not know how to handle this. Any help is appreciated.

Have a good day!

You're stating that the return value is ObjectID, but in fact it is an object of the shape {_id: ObjectId}. I don't know how to fix it (hence the comment and not an answer), but the error must be here. - Cerberus
According to Collection documentation. "A Collection can be parameterized with any type that implements the Serialize and Deserialize traits from the serde crate." Since ObjectId also implements Serialize and Deserialize this should not be a problem. - marknikky