Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectId error #1224

Closed
JonathanWoollett-Light opened this issue Oct 11, 2024 · 2 comments
Closed

ObjectId error #1224

JonathanWoollett-Light opened this issue Oct 11, 2024 · 2 comments
Assignees
Labels

Comments

@JonathanWoollett-Light
Copy link

JonathanWoollett-Light commented Oct 11, 2024

Versions/Environment

  1. What version of Rust are you using? rustc 1.81.0 (eeb90cda1 2024-09-04)
  2. What operating system are you using? Windows
  3. What versions of the driver and its dependencies are you using? (Run
    cargo pkgid mongodb & cargo pkgid bson)
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version()): mongodb-win32-x86_64-windows-7.0.14/bin/mongod.exe
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? Standalone

Describe the bug

Within my application I have

    info!("user_id:\n\t{user_id}\n\t{user_id:?}");
    let find_doc = doc! { "_id": user_id };
    info!("find_doc: {find_doc:?}");
    let all_users = users.find(doc!{}).await.unwrap().collect::<Vec<_>>().await;
    info!("all_users: {all_users:?}");
    let user = users
        .find_one(find_doc)
        .await
        .map_err(PickError::QueryUser)?
        .ok_or(PickError::FindUser)?;

upon reaching and executing this in a test I get the error:

500 Internal Server Error, Failed to query user: Kind: invalid value: string "ObjectId(\"670996bba2c4b6ec183babff\")", expected 24-character, big-endian hex string, labels: {}

The notable section here being that it reports the ObjectId as 670996bba2c4b6ec183babff rather than 670996baa2c4b6ec183babfe.

The logs are:

2024-10-11T21:20:59.870511Z  INFO server::pick: user_id:
        670996baa2c4b6ec183babfe
        ObjectId("670996baa2c4b6ec183babfe")
2024-10-11T21:20:59.870662Z  INFO server::pick: find_doc: Document({"_id": ObjectId("670996baa2c4b6ec183babfe")})
2024-10-11T21:20:59.871112Z  INFO server::pick: all_users: [Err(Error { kind: BsonDeserialization(DeserializationError { message: "invalid value: string \"ObjectId(\\\"670996bba2c4b6ec183babff\\\")\", expected 24-character, big-endian hex string" }), labels: {}, wire_version: None, source: None })]

Checking the database with Mongdb Compass I can see:

{
  "_id": {
    "$oid": "670996baa2c4b6ec183babfe"
  },
  ...
}
@JonathanWoollett-Light
Copy link
Author

JonathanWoollett-Light commented Oct 11, 2024

Updating to use

    let users = database.collection::<DatabaseUser>(USER_COLLECTION);
    let doc_users = database.collection::<bson::Document>(USER_COLLECTION);
    info!("user_id:\n\t{user_id}\n\t{user_id:?}");
    let find_doc = doc! { "_id": user_id };
    info!("find_doc: {find_doc:?}");
    let all_users = users.find(doc!{}).await.unwrap().collect::<Vec<Result<DatabaseUser,_>>>().await;
    info!("all_users: {all_users:?}");
    let all_users = doc_users.find(doc!{}).await.unwrap().collect::<Vec<Result<bson::Document,_>>>().await;
    info!("all_users: {all_users:?}");

outputs

2024-10-11T22:01:57.010523Z  INFO server::pick: user_id:
        6709a0539df267ff93605156
        ObjectId("6709a0539df267ff93605156")
2024-10-11T22:01:57.010709Z  INFO server::pick: find_doc: Document({"_id": ObjectId("6709a0539df267ff93605156")})
2024-10-11T22:01:57.011435Z  INFO server::pick: all_users: [Err(Error { kind: BsonDeserialization(DeserializationError { message: "invalid value: string \"ObjectId(\\\"6709a0549df267ff93605157\\\")\", expected 24-character, big-endian hex string" }), labels: {}, wire_version: None, source: None })]
2024-10-11T22:01:57.012132Z  INFO server::pick: all_users: [Ok(Document({"_id": ObjectId("6709a0539df267ff93605156"), .. }))]

which suggests the problem is in https://github.com/mongodb/bson-rust

since

#[derive(Debug, Serialize, Deserialize)]
pub struct DatabaseUser {
    pub _id: ObjectId,
    pub pictures: BTreeMap<ObjectId, Glicko2Rating>,
    // ...
}

diving a little further there is a pictures field that contains ObjectIds which match the id returned in the error so it seems this is where the deserialization issue arises.

{
  "_id": {
    "$oid": "6709a1840466a4d817174c6c"
  },
  "email": "a",
  "hash": "$argon2id$v=19$m=19456,t=2,p=1$pY0WSJ4qyxKwQuE7PM84VA$pAUXkdPaBRuzFZizeYZTBc7lVvjsMz8Hf9TChm8MYPM",
  "pictures": {
    "ObjectId(\"6709a1850466a4d817174c6d\")": {
      "rating": 1500,
      "deviation": 350,
      "volatility": 0.06
    },
    "ObjectId(\"6709a1850466a4d817174c6e\")": {
      "rating": 1500,
      "deviation": 350,
      "volatility": 0.06
    },
    "ObjectId(\"6709a1850466a4d817174c6f\")": {
      "rating": 1500,
      "deviation": 350,
      "volatility": 0.06
    },
    "ObjectId(\"6709a1850466a4d817174c70\")": {
      "rating": 1500,
      "deviation": 350,
      "volatility": 0.06
    }
  ...
}

@JonathanWoollett-Light
Copy link
Author

An example JonathanWoollett-Light/testing@c965159 which can be run simply with cargo run produces a related error called Result::unwrap()on anErr value: Error { kind: BsonSerialization(InvalidDocumentKey(Document({}))), labels: {}, wire_version: None, source: None }.

Okay I think the point is in essence that using ObjectIds for document keys is invalid and my program was doing this inadvertently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants