Error returned after an update, but the update was successful

Hi,

I’m using the Tokio async runtime v2.0.1, and my DB version is 4.4. I have an update that executes, and the data successfully updates, but I’m returned an error that isn’t making sense. The error is:

Err(Error { kind: BsonDeserialization(DeserializationError { message: "missing field `enable`" }), labels: {} })

There is an enable field in the document, but it isn’t one of the fields I was updating.

Here is the code for the actual update:

let update: UpdateModifications = UpdateModifications::Document(doc! {
        "$set": doc! {
              "system.version": 4
         }
    });

The actual db update is:

collection
            .find_one_and_update(filter, update, options)
            .await?

options happens to be:

let options = FindOneAndUpdateOptions::builder().upsert(false).build();

As I said, the update works, but the error comes back stating that the enable field is missing. The document in the db does have a lot of other fields, but I’m only updating one of them.

Thanks

Hi Mark! Have you verified that the enable field is present in the document? The error you’re encountering does indicate that the data being returned from find_one_and_update does not contain the enable field. I recommend running this same operation in the shell or on a Collection<Document> and inspecting the data returned. You can use the clone_with_type method on your current collection to retrieve a Collection<Document>. You may need to adjust your filter to ensure you’re finding the correct document.

Alternatively, if you can’t guarantee that each document you’re retrieving from the collection will contain an enable field, you can adjust the corresponding field in the struct/enum you’re using to model your data to be an Option, which will allow for proper deserialization to None when the field is not present.

Please let us know if this helps or if you run into any further issues!

1 Like

Hi,

Thanks - I made fields into < Option >'s and the error has stopped.

Thank you for the prompt and detailed reply.

Mark

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.